Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

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.

Filter by
Sorted by
Tagged with
10 votes
4 answers
943 views

Without ;;&, /bin/sh gives Syntax error: ")" unexpected (expecting ";;"). ;;& triggers shellcheck's ^-- SC2127 (error): To use cases with ;;&, specify #!/usr/bin/env ...
Swudu Susuwu's user avatar
1 vote
3 answers
355 views

I need to extract information from a log file that is deleted and recreated every time a program runs. After detecting that the file exists (again), I would like to tail it for a certain regexp. The ...
user2066480's user avatar
0 votes
1 answer
156 views

I'm very simply wondering if, in a bash script, a new line is functionally 100% equivalent to &&? e.g.: #!/bin/bash 7z x "${file}" mv "${file}" "${new_file}" ...
s.k's user avatar
  • 521
2 votes
4 answers
167 views

I want to execute commandA which prints the following: line1 line2 line3 started successfully line4 ... ... And when the line started successfully is printed I want to start another command (and ...
Marinos An's user avatar
5 votes
3 answers
985 views

I want to detect errors in application's execution logic. E.g.: forgot to call free() on address returned by malloc() did not close file handle returned by open() invalid flags passed to open() ...
zomega's user avatar
  • 1,022
0 votes
0 answers
29 views

The last elif arm does not get executed: #!/usr/bin/bash searches=("feet" "axilas" "lactant") length=${#searches[@]} searchFunction() { echo "Enter the respective ...
John Smith's user avatar
0 votes
0 answers
15 views

in my script an if statement returns: line 3: []: command not found the statement: if ["$(pidof -x $(basename $0) -o %PPID)"]; then echo process already running; exit; fi what I tried: ...
andy's user avatar
  • 1
6 votes
1 answer
8k views

I am working on a shell script and decided to check my work via shellcheck.net. I am able to get functionally the same behavior of the following two lines in my script: findmnt /dev/sda1 >/dev/null ...
Kahn's user avatar
  • 1,827
2 votes
1 answer
427 views

I often execute raw versions of remote Bash scripts in GitHub with this pattern: wget -O - https://raw.githubusercontent.com/<username>/<project>/<branch>/<path>/<file> | ...
variable_expander's user avatar
0 votes
1 answer
45 views

Goal: less the contents (from a variable) and then ask the user whether the contents should be saved to a file (i.e., .recovered_object) only after the user quits out of less. The excerpt from my ...
Per48edjes's user avatar
0 votes
2 answers
342 views

I am working on an automated pull request check via GitHub actions and I want to preserve some data from a command's stderr output between jobs. For this, I need to write the stderr to an artifact ...
Abhishek Jain's user avatar
3 votes
5 answers
3k views

One feature of Perl that I really like is its generalization of looping control keywords to any curly-brace-delimited lexical block1. For example, one can use Perl's last directive to exit any such ...
kjo's user avatar
  • 16.4k
4 votes
2 answers
5k views

I'm currently refactoring a script which has slowly grown beyond control. I'm trying to spin off repetition into functions. However, I have a repeated test being called from a loop, and want it to ...
Stripy42's user avatar
3 votes
2 answers
15k views

$!/bin/sh if grep "$1" /etc/passwd 2>/dev/null #Search username at beging of line1 then echo "Pattern found - Job Over" else echo "Pattern not found" fi
Shridhar B Waghamare's user avatar
7 votes
2 answers
1k views

I am studying Linux device drivers, my main focus is on wifi drivers. I want to know how the code flows when I plugin my device. Maybe, I can do something like add a printk line in every function. ...
Akshdeep Singh's user avatar
2 votes
1 answer
2k views

I've got a while loop that uses vared to prompt for user input. I am looking for a way to have it timeout, execute a default variable and loop back to the prompt if there is not user input after a ...
Кафка's user avatar
0 votes
2 answers
2k views

I want to count the number of lines in a pipe and then continue the pipe depending on the outcome. I tried x=$(printf 'faa\nbor\nbaz\n' \ | tee /dev/stderr | wc -l) 2>&1 \ | if [[ $x -ge ...
Lucas's user avatar
  • 2,955
3 votes
2 answers
331 views

I wanted to make a list of similar clear commands clearer to read, so I've made a little terminal loop for what in \ cache \ thumbs \ ; do my template $what:clear; done It works great, however ...
jave.web's user avatar
  • 162
1 vote
3 answers
2k views

I think my question is similar to this one but with one extrat step - say I have 3 scripts: main_script.sh report_success.sh report_failure.sh How can I do something like this pseudo-code does: if (...
user3768495's user avatar
7 votes
1 answer
2k views

I work on a script of about 20 lines which I find myself testing time and again by copy-pasting and executing line by line. Instead of copying-pasting each line and hitting Enter to execute, I would ...
user avatar
67 votes
5 answers
80k views

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 ...
user avatar
-1 votes
2 answers
1k views

This is a follow up to this question; I don't know why but I keep misunderstanding the following code, although I try very hard to understand it: function read_and_verify { read -p "$1:" tmp1 ...
user avatar
1 vote
1 answer
4k views

I have a txt files with all the file names I need to analyze. I have this file (inputFile.txt) : /path/file1a path/file1b /path/file2a path/file2b i have a code which needs two two input files in ...
IP25's user avatar
  • 11
3 votes
2 answers
92 views

I was writing some "if then" statements and found what seemed to me an odd behavior. Upon investigation I realized that it boiled down to the exit code of the comparison I was making. I illustrate my ...
Elegance's user avatar
13 votes
6 answers
17k views

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" ] && [ -...
Elegance's user avatar
  • 133
0 votes
1 answer
123 views

I use Debian and Apache and say I have many virtual host files (above 20) and I want all of them to include this line in the end: Alias /phpmyadmin /usr/share/phpmyadmin The reason is to allow to ...
user avatar
7 votes
2 answers
6k views

Expanding from this question, we have a use case where we want to pipe the stdout of a command depending on whether that command succeeded or failed. We start with a basic pipe command | grep -P "...
I'll Eat My Hat's user avatar
1 vote
2 answers
292 views

I'm trying to write a script that searches through a pre-made list of running processes across a series of machines. I'm specifically looking for rsyslogd running on those devices, and attempting to ...
BigDamnHero's user avatar
0 votes
4 answers
955 views

I have a file with Server and Domain names in it as below. Names.txt : ABCDomain ContractABCServer_1 ABCDomain ABC_server1 LinkDomain CoreLinkServer_1 TADDomain TADServer_1 (I'm getting above file ...
Aditya Telang's user avatar
4 votes
2 answers
3k views

I have a file to which I want to append some content (including the first, second and third empty spaces visible in my code below): ### I am text 1 ### I am text 2 (The actual text I append is way ...
Arcticooling's user avatar
  • 4,523
-1 votes
5 answers
1k views

I use the following code which is part of this script that I use to update my WordPress websites: #!/bin/bash drt="/var/www/html" for dir in ${drt}/*/; do if pushd "$dir"; then wp plugin ...
Arcticooling's user avatar
  • 4,523
2 votes
1 answer
1k views

To run Rust program with a backtrace one should set environment variable RUST_BACKTRACE to one and run the program, so my first guess as inexperienced bash user was: $ RUST_BACKTRACE=1 && ...
gasabr's user avatar
  • 23
1 vote
2 answers
2k views

I'm using a keycloak server, when I run this command: standalone.sh This command launchs the server and I'm not able to stop it until I execute Ctrl-C command. I though about runing an instruction ...
Slim's user avatar
  • 113
0 votes
1 answer
5k views

I stored the following script in a file and created an alias to that file in the user's bashrc, then sourced that bashrc: #!/bin/bash domain="$1" && test -z "$domain" && exit 2 ...
Arcticooling's user avatar
  • 4,523
11 votes
2 answers
10k views

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 ...
Arcticooling's user avatar
  • 4,523
36 votes
2 answers
8k views

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 ...
user270650's user avatar
-2 votes
3 answers
1k views

What will be an "elegant" one-line way to append a single line of data into the end of a file, with herestring, if this exact data isn't already in that file? This is my herestring append pattern: ...
Arcticooling's user avatar
  • 4,523
52 votes
4 answers
265k views

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 ...
Viesturs's user avatar
  • 993
9 votes
2 answers
24k views

Perhaps this applies to more than just bash, but I am confused about the role of brackets in if-statements. Most examples seem to have the following format if [ expression ]; then #do stuff fi ...
dkv's user avatar
  • 477
2 votes
5 answers
10k views

Here is what I have so far: #!/bin/bash for file in $PATH ; do # Scanning files in $PATH if [ -x ] ; then #Check if executable echo "Executable File" else ...
Tyler's user avatar
  • 21
1 vote
2 answers
91 views

I am writing a script called pickanumber.sh. I was setting up the script so I will ask the user to pick a number. If the number that they type is not "8" than the script will continue running. I can't ...
jose R's user avatar
  • 11
31 votes
4 answers
7k views

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 || ...
Subhaa Chandar's user avatar
-1 votes
1 answer
3k views

In for (( expr1 ; expr2 ; expr3 )) ; do commands ; done expr1, expr2, and expr3 are arithmetic expressions. Is expr1 ; expr2 ; expr3 not an arithmetic expression? (( expr1 ; expr2 ; expr3 )) isn't a ...
Tim's user avatar
  • 107k
0 votes
1 answer
1k views

I have a list of text files in a directory which I want to search from terminal. To not make myself think too much, I want to be able to search for few words, example search one two three if there is ...
relidon's user avatar
  • 115
2 votes
1 answer
358 views

So I guess there is actually two parts to this question, because I don't want the RM to go untill the nohup command has finished, but the problem is, I love to string together commands, but my nohup ...
FreeSoftwareServers's user avatar
4 votes
2 answers
26k views

I can do this in bash: while read -n1 -r -p "choose [y]es|[n]o" do if [[ $REPLY == q ]]; then break; else #whatever fi done which works but seems a bit redundant, can ...
razzak's user avatar
  • 297
1 vote
3 answers
2k views

I would like to run a for loop for a set of variables which I incorrectly denote as {A,B}. Following that my script looks like: #!/bin/sh for {A,B} in {1,2} {3,4} {5,6} {7,8} do echo A=$A B=$B C=$(($...
hbaromega's user avatar
  • 171
3 votes
5 answers
1k views

I have a Bash script that looks like #!/bin/bash # FECHA=`date +%j` if [ $FECHA -eq 40 ] then echo "Esta semana le toca preparar el café a Osvaldo" | mail -s 'Café' [email protected] ...
murpholinox's user avatar
442 votes
3 answers
230k views

I often see tutorials online that connect various commands with different symbols. For example: command1 | command2 command1 & command2 command1 || command2 command1 && command2 ...
terdon's user avatar
  • 253k
109 votes
9 answers
152k views

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 ...
rubo77's user avatar
  • 30.6k