Shell programming (2)

for in loop statement

#!/bin/bash
for x in one two three four
do
        echo number $x
done

Example: Take out each line of name in passwd and output hello + name

#!/bin/bash
LINES=`wc -l /home/eko/passwd | cut -d' ' -f1`
for i in `seq 1 $LINES`
do
        echo "hello,`head -n $i /home/eko/passwd | tail -n 1 | cut -d: -f1`"
done

*seq statement 

root @ ubuntu: / home / eko # seq 1 5
1
2
3
4
5

root @ ubuntu: / home / eko # seq 1 2 10
1
3
5
7
9

 for loop

#!/bin/bash
for((i=1;i<10;i++))
do
echo "hello $i"
done

 for file in

#!/bin/bash  
  
for file in /proc/*;  
do  
echo $file is file path \! ;  
done   


#!/bin/bash  
  
for file in $(ls *.sh)  
do  
echo $file is file path \! ;  
done  

 

while

while [ $count -le 6 ]; do
    echo $count
    count=$((count + 1))
done
echo "finished"

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325291276&siteId=291194637