数学思想方法-分布式计算-linux/unix技术基础(5)

时间:2022-09-04 10:33:43

shell命令行参数

-bash-4.2$ cat test1.sh
#!/bin/sh
echo "$0  "
echo "$1  "
echo "$2  "

-bash-4.2$ ./test1.sh a b c
./test1.sh  
a  
b  
-bash-4.2$

本博客全部内容是原创,假设转载请注明来源

http://blog.csdn.net/myhaspl/

显示全部命令行參数

-bash-4.2$ cat test1.sh
#!/bin/sh
until [ -z "$1" ]
do
   echo "$1  "
   shift
done
-bash-4.2$ ./test1.sh a b c d e f
a  
b  
c  
d  
e  
f

-bash-4.2$ cat test1.sh
#!/bin/sh
index=1
for myarg in $*
do
   echo "NO#$index=$myarg"
   let "index+=1"
done
-bash-4.2$ ./test1.sh a b c d e f
NO#1=a
NO#2=b
NO#3=c
NO#4=d
NO#5=e
NO#6=f
-bash-4.2$

条件表达式

-bash-4.2$ cat test1.sh
#!/bin/sh
a=1
b=2
if [ $a -gt $b ]
then
   echo "GT"
else
   echo "LT"
fi
-bash-4.2$ ./test1.sh
LT
-bash-4.2$

-bash-4.2$ cat test1.sh
#!/bin/sh
a=2
b=2
if [ $a -gt $b ]
then
   echo "GT"
elif [ $a -eq $b ]
then
   echo "eq"
else
   echo "LT"
fi
-bash-4.2$ ./test1.sh
eq
-bash-4.2$

-bash-4.2$ cat test1.sh
#!/bin/sh
echo "====================="
echo "1.a"
echo "2.b"
echo "3.c"
read  mychoice
case $mychoice in
     1 ) echo "a";;
     2 ) echo "b";;
     3 ) echo "c";;
esac
exit 0
-bash-4.2$ ./test1.sh
=====================
1.a
2.b
3.c
2
b
-bash-4.2$

循环

-bash-4.2$ cat test1.sh
#!/bin/sh
for filename in `ls`
do
    echo $filename
done
-bash-4.2$ ./test1.sh
1
abc
abd
error.log
hadoop-2.4.1
hadoop-2.4.1-src.tar.gz
hadoop-2.4.1.tar.gz
hello
mydoclist

-bash-4.2$