Linux--shellプログラミング--ループ構造
3669 ワード
while文
while
do
done
# or
while ; do
done
#! /bin/bash
# while.sh
while [ $1 ]
do
if [ -f $1 ];then
echo -e "
display:$1"
cat $1
else
echo "$1 is not a file name."
fi
shift
done
until文
until
do
done
# or
until ; do
done
#! /bin/bash
# until.sh
until [ -z "$2" ]; do
cp $1 $2
shift 2
done
if [ -n "$1" ]; then
echo "bad parameter!"
fi
for文
for [in ]
do
done
# or
for [in ]; do ; done
#! /bin/bash
# for.sh
for day in Monday Wednesday Friday Sunday
do
echo $day
done
#! /bin/bash
# for_test.sh
week={Mon Tue Wed}
for i in "${week[@]}"; do echo $i; done
#! /bin/bash
for in
do
done
#
for file in *.sh; do wc -w $file; done
for ((e1; e2; e3)); do ; done
# or
for ((e1; e2; e3)); do
done
break,continue,exit
#! /bin/bash
# break.sh
num=$1
while true; do
echo -n "$num"
if ((--num == 0)); then
break;
fi
done
echo "bye"
#! /bin/bash
# continue.sh
num=$1
for ((i=0; i
exitコマンド
select文
select identifier [in word...]
do
done
#! /bin/bash
# select.sh
PS3="Choice? "
select choice in query add delete update exit
do
case "$choice" in
query) echo "Call query routine"; break;;
add) echo "Call add routine"; break;;
delete) echo "Call delete routine"; break;;
update) echo "Call update routine"; break;;
exit) echo "Call exit routine"; break;;
esac
done
echo "you input $REPLY; your choice is: $choice"
echo "bye"