Questions tagged [control-flow]

Control flow refers to the order that computer code is executed in when a program or script is running. Examples include loops (code is repeated) and conditionals where one branch is run instead of another. Use this tag for questions about control flow in scripts or programs – not questions about terminal flow control.

Control flow refers to the order that computer code is executed in when a program or script is running. Examples include loops (code is repeated) and conditionals where one branch is run instead of another.

Further reading

80 questions
538
votes
10 answers

How to conditionally do something if a command succeeded or failed

How can I do something like this in bash? if "`command` returns any error"; then echo "Returned an error" else echo "Proceed..." fi
Shinmaru
420
votes
3 answers

What are the shell's control and redirection operators?

I often see tutorials online that connect various commands with different symbols. For example: command1 | command2 command1 & command2 command1 || command2 command1 && command2 Others seem to be connecting commands to files: command1 >…
terdon
  • 234,489
  • 66
  • 447
  • 667
132
votes
6 answers

Confusing use of && and || operators

I was skimming through an /etc/rc.d/init.d/sendmail file (I know this is hardly ever used, but I'm studying for an exam), and I've become a bit confused about the && and the || operators. I've read where they can be used in statements such as: if […
josh-cain
  • 1,781
  • 3
  • 16
  • 13
108
votes
9 answers

Press space to continue

How do I stop a bash script until a user has pressed Space? I would like to have the question in my script Press space to continue or CTRL+C to exit and then the script should stop and wait until Space is pressed.
rubo77
  • 27,777
  • 43
  • 130
  • 199
86
votes
6 answers

How to loop over the lines of a file?

Say I have this file: hello world hello world This program #!/bin/bash for i in $(cat $1); do echo "tester: $i" done outputs tester: hello tester: world tester: hello tester: world I'd like to have the for iterate over each line individually…
Tobias Kienzler
  • 9,184
  • 13
  • 65
  • 106
50
votes
4 answers

Test if a string contains a substring

I have the code file="JetConst_reco_allconst_4j2t.png" if [[ $file == *_gen_* ]]; then echo "True" else echo "False" fi I test if file contains "gen". The output is "False". Nice! The problem is when I substitute "gen" with a variable…
Viesturs
  • 933
  • 3
  • 10
  • 15
49
votes
5 answers

What is "declare" in Bash?

After reading ilkkachu's answer to this question I learned on the existence of the declare (with argument -n) shell built in. help declare brings: Set variable values and attributes. Declare variables and give them attributes. If no NAMEs are…
user149572
38
votes
8 answers

How do I reverse a for loop?

How do I properly do a for loop in reverse order? for f in /var/logs/foo*.log; do bar "$f" done I need a solution that doesn't break for funky characters in the file names.
user541686
  • 3,033
  • 5
  • 28
  • 43
35
votes
2 answers

Bash "for" loop without a "in foo bar..." part

I was recently looking at some code that confused me because it works and I didn't expect it to. The code reduces to this example #!/bin/bash for var; do echo "$var" done When run with command line arguments is prints them $ ./test a b…
user270650
  • 351
  • 3
  • 3
31
votes
2 answers

do...while or do...until in POSIX shell script

There is the well known while condition; do ...; done loop, but is there a do... while style loop that guarantees at least one execution of the block?
Shawn J. Goff
  • 45,338
  • 25
  • 134
  • 145
31
votes
4 answers

Are if else statement equivalent to logical and && or || and where should I prefer one over the other?

I'm learning about decision making structures and I came across these codes: if [ -f ./myfile ] then cat ./myfile else cat /home/user/myfile fi [ -f ./myfile ] && cat ./myfile || cat /home/user/myfile Both of them behave the same. Are…
Subhaa Chandar
  • 343
  • 3
  • 4
24
votes
3 answers

Loop through the lines of two files in parallel

The object of the script I'm making is to compare two series of files. The file names are themselves stored into two separate files, one path per line. My idea is to have two while read loops, one for each list of file names, but how can I mix the…
mkrouse
  • 929
  • 4
  • 14
  • 20
12
votes
3 answers

Send task to background in an "if"

Why is this? if true; then sleep 3 &; fi bash: syntax error near unexpected token `;' I want to run sleep 3 in the background so that the command ["sleep 3" is just an example] would run in "paralell" style, so it finishes faster. But I'm…
LanceBaynes
  • 39,295
  • 97
  • 250
  • 349
11
votes
6 answers

Checking for the existence of multiple directories

I want to check for the existence of multiple directories, say, dir1, dir2 and dir3, in the working directory. I have the following if [ -d "$PWD/dir1" ] && [ -d "$PWD/dir2" ] && [ -d "$PWD/dir3" ]; then echo True else echo False fi But I…
Elegance
  • 113
  • 1
  • 1
  • 5
11
votes
2 answers

A while loop and an here-document - what happens when?

I have this while loop and here-document combo which I run in Bash 4.3.48(1) and I don't understand its logic at all. while read file; do source ~/unwe/"$file" done <<-EOF x.sh y.sh EOF My question is comprised of these parts: What does…
Arcticooling
  • 1
  • 12
  • 44
  • 103
1
2 3 4 5 6