Difference between revisions of "Bash loops"

From Tech-Wiki
Jump to: navigation, search
 
(6 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
Executing a Do loop to repeat one command with different hosts
 
Executing a Do loop to repeat one command with different hosts
  # for n in {1..5}; do ssh host0$n "shutdown"; done
+
  # for num in {1..5}; do ssh host-$num "shutdown"; done
 +
# for host in `cat hosts.txt`; do ssh $host "shutdown"; done
 +
# while read LINE; do COMMAND $LINE; done < FILE
 +
 
 +
Executing something based on command's output
 +
# for x in $(cmd1) ; do cmd2 $x ; done
 +
# while read x ; do cmd2 $x ; done < <(cmd1)
 +
# while read x ; do cmd $x ; done < file.txt
  
 
Executing the same command forever
 
Executing the same command forever
 
  # while true; do date +%s; done
 
  # while true; do date +%s; done

Latest revision as of 22:10, 26 June 2021


Executing a Do loop to repeat one command with different hosts

# for num in {1..5}; do ssh host-$num "shutdown"; done
# for host in `cat hosts.txt`; do ssh $host "shutdown"; done
# while read LINE; do COMMAND $LINE; done < FILE

Executing something based on command's output

# for x in $(cmd1) ; do cmd2 $x ; done
# while read x ; do cmd2 $x ; done < <(cmd1)
# while read x ; do cmd $x ; done < file.txt

Executing the same command forever

# while true; do date +%s; done