UsiScript
for loop in mode iniline
Code
# For loop in one line # for i in {1..5}; do COMMAND-HERE; done # Example 1 for((i=1;i<=10;i+=2)); do echo "Welcome $i times"; done # Example 2 = display the list of the files in the curretn folder cpt=1 ; for i in *; do echo $cpt : $i && readlink -f $i && cpt=$(expr $cpt + 1); done
test if in mode iniline
Code
# If statement in one line # if [ foo ]; then a && b; elif [ bar ]; c && d; else e && f; fi # cmd1 && cmd2 || echo fail # if cmd1 ; then cmd2 ; else echo fail ; fi # Simple : check if the file exist. If not display a message and exit script if [ ! -f output.log ]; then echo "log file not found" && exit 1 ; fi # Example 1 line=$(tail -n1 output.log ); if [ $? -ne 0 ]; then line="NO log" && echo "log not found"; else echo "log file founed"; fi # Example 2 : multiple if in one line (line=$(tail -n1 output.log ); if [ $? -ne 0 ]; then line="NO log"; fi) && (if [ "$line" == "OK" ]; then echo "log OK"; else echo "log KO"; fi) # Example 3 [ $(date +%w) -eq 6 ] && echo "do something on Saturdays" || echo "do different things on other days"
while loop in mode iniline
Code
# While loop in one line # while [ CONDITION_STATEMENT ]; do SOME_CODE; done # Example 1 theRESULT=0 while [ $theRESULT -lt 1 ]; do echo "theRESULT still 0" && theRESULT=$(ls -l|wc -l) && echo "theRESULT = $theRESULT" ; done