mirror of
https://github.com/onceupon/Bash-Oneliner.git
synced 2024-11-29 15:46:35 +00:00
edit loop section
add more examples for 'if'
This commit is contained in:
parent
8309dbb316
commit
c05d867f88
82
README.md
82
README.md
@ -880,71 +880,75 @@ find . -name "*.mso" -size -74c -delete
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Loops
|
## Loops
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-oneliner-commands)]
|
||||||
##### While loop, column subtraction of a file (e.g. a 3 columns file)
|
|
||||||
|
|
||||||
```bash
|
|
||||||
while read a b c; do echo $(($c-$b));done < <(head filename)
|
|
||||||
|
|
||||||
#there is a space between the two '<'s
|
|
||||||
```
|
|
||||||
##### While loop, sum up column subtraction
|
|
||||||
|
|
||||||
```bash
|
|
||||||
i=0; while read a b c; do ((i+=$c-$b)); echo $i; done < <(head filename)
|
|
||||||
```
|
|
||||||
|
|
||||||
##### While loop, keep checking a running process (e.g. perl) and start another new process (e.g. python) immetiately after it. (BETTER use the wait command! Ctrl+F 'wait')
|
|
||||||
|
|
||||||
```bash
|
|
||||||
while [[ $(pidof perl) ]];do echo f;sleep 10;done && python timetorunpython.py
|
|
||||||
```
|
|
||||||
|
|
||||||
##### If loop
|
##### If loop
|
||||||
```bash
|
```bash
|
||||||
# if and else loop for string matching
|
# if and else loop for string matching
|
||||||
if [[ "$c" == "read" ]]; then outputdir="seq"; else outputdir="write" ; fi
|
if [[ "$c" == "read" ]]; then outputdir="seq"; else outputdir="write" ; fi
|
||||||
```
|
|
||||||
|
|
||||||
if (($j==$u+2))
|
# Test if myfile contains the string 'test':
|
||||||
#(( )) use for arithmetic operation
|
if grep -q hello myfile; then …
|
||||||
```
|
|
||||||
|
|
||||||
```bash
|
# Test if mydir is a directory, change to it and do other stuff:
|
||||||
if [[$age >21]]
|
if cd mydir; then
|
||||||
#[[ ]] use for comparison
|
echo 'some content' >myfile
|
||||||
```
|
else
|
||||||
|
echo >&2 "Fatal error. This script requires mydir."
|
||||||
|
fi
|
||||||
|
|
||||||
##### Test if file exist
|
# Test if file exist
|
||||||
```bash
|
|
||||||
if [ -e 'filename' ]
|
if [ -e 'filename' ]
|
||||||
then
|
then
|
||||||
echo -e "file exists!"
|
echo -e "file exists!"
|
||||||
fi
|
fi
|
||||||
```
|
|
||||||
|
|
||||||
##### if else Test if file exist
|
# Test if file exist but also including symbolic links:
|
||||||
```bash
|
if [ -e myfile ] || [ -L myfile ]
|
||||||
if [ -e $filename ]; then echo -e "file exists!"; else mkdir $filename; fi
|
then
|
||||||
|
echo -e "file exists!"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test if the value of x is greater or equal than 5
|
||||||
|
if [ "$x" -ge 5 ]; then …
|
||||||
|
|
||||||
|
# Test if the value of x is greater or equal than 5, in bash/ksh/zsh:
|
||||||
|
if ((x >= 5)); then …
|
||||||
|
|
||||||
|
# Use (( )) for arithmetic operation
|
||||||
|
if (($j==$u+2))
|
||||||
|
|
||||||
|
# Use [[ ]] for comparison
|
||||||
|
if [[$age >21]]
|
||||||
```
|
```
|
||||||
|
|
||||||
##### For loop
|
##### For loop
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
for i in $(ls); do echo file $i;done
|
for i in $(ls); do echo file $i;done
|
||||||
```
|
|
||||||
|
|
||||||
##### for loop, press any key to continue each loop
|
# Press any key to continue each loop
|
||||||
```bash
|
|
||||||
for i in $(cat tpc_stats_0925.log |grep failed|grep -o '\query\w\{1,2\}');do cat ${i}.log; read -rsp $'Press any key to continue...\n' -n1 key;done
|
for i in $(cat tpc_stats_0925.log |grep failed|grep -o '\query\w\{1,2\}');do cat ${i}.log; read -rsp $'Press any key to continue...\n' -n1 key;done
|
||||||
```
|
|
||||||
|
|
||||||
##### for loop, print a file line by line when a key is pressed
|
# Print a file line by line when a key is pressed
|
||||||
```bash
|
|
||||||
for line in $(cat myfile); do echo $line; read -n1; done
|
for line in $(cat myfile); do echo $line; read -n1; done
|
||||||
```
|
```
|
||||||
|
|
||||||
|
##### While loop,
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Column subtraction of a file (e.g. a 3 columns file)
|
||||||
|
while read a b c; do echo $(($c-$b));done < <(head filename)
|
||||||
|
#there is a space between the two '<'s
|
||||||
|
|
||||||
|
# Sum up column subtraction
|
||||||
|
i=0; while read a b c; do ((i+=$c-$b)); echo $i; done < <(head filename)
|
||||||
|
|
||||||
|
# Keep checking a running process (e.g. perl) and start another new process (e.g. python) immetiately after it. (BETTER use the wait command! Ctrl+F 'wait')
|
||||||
|
while [[ $(pidof perl) ]];do echo f;sleep 10;done && python timetorunpython.py
|
||||||
|
```
|
||||||
|
|
||||||
##### switch (case in bash)
|
##### switch (case in bash)
|
||||||
```bash
|
```bash
|
||||||
read type;
|
read type;
|
||||||
|
Loading…
Reference in New Issue
Block a user