Shell 脚本编程【循环】

#!/bin/bash

clear

for var in Cloks Alabama Alaska Arizona Arkansas California Colorado
do
  echo $var
done | sort

# for var in Cloks Alabama Alaska Arizona Arkansas California Colorado; do echo $var; done | sort

echo $var

sleep 1 && clear

list="Alabama Alaska Arizona Arkansas California Colorado"
# list = append(list, "Connecticut")
list=$list" Connecticut"

for state in $list; do
  echo "have you ever visited $state?"
done

sleep 1 && clear

# reading values from a file

file="./states"

cat << EOF > $file
Alabama cloaks
Alaska
Arizona
Arkansas
California
Colorado
EOF

IFS_OLD=$IFS
IFS=$'\n'

for state in $(cat $file); do
  echo "Visit beautiful $state"
done

IFS=$IFS_OLD

rm -rf $file

sleep 1 && clear

for file in ./*; do
  if [ -d "$file" ];then
    echo "$file is a directory"
  elif [ -f "$file" ];then
      echo "$file is a file"
  fi
done

sleep 1 && clear

for (( i = 1; i <= 10; i++ )); do
  echo "the number is $i"
done

sleep 1 && clear

var1=10

while [ $var1 -gt 0 ]; do
    echo $var1
    var1=$[ $var1 - 1 ]
done

sleep 1 && clear

var1=10

while echo $var1
  [ $var1 -ge 0 ]; do
    echo "this is inside loop"
    var1=$[ $var1 - 1 ]
done

sleep 1 && clear

var1=10

until [ $var1 -eq 0 ]; do
    echo $var1
    var1=$[ $var1 - 1 ]
done

IFS_OLD=$IFS
IFS=$'\n'
for entry in $(cat /etc/passwd); do
  echo "Values in $entry -"
  IFS=:
  for value in $entry; do
    echo " $value"
  done >> output.txt
done > output-1.txt

IFS=$IFS_OLD

sleep 1 && clear

for (( i = 0; i < 10; i++ )); do
    echo "start loop $i"
    for (( j = 0; j < i; j++ )); do
       if [ $i -gt 5 ]; then
         break 2
       else
         echo $i-$j
       fi
    done
done

sleep 1 && clear

IFS_OLD=$IFS
IFS=:

for folder in $PATH; do
  echo "start folder: $folder"
  for file in $folder/*; do
    if [ -x $file ]; then
      echo "    $file"
    fi
  done
done

IFS=$IFS_OLD

sleep 1 && clear

input="users.csv"

cat << EOF > $input
rich,Richard Blum
christine,Christine Bresnahan
barbara,Barbara Blum
tim,Timothy Bresnahan
EOF

while IFS=',' read -r userid name; do
  echo "adding $userid-$name"
#  useradd -c "$name " -m $userid
done < "$input"

rm $input