shell訓練day7 8.21

•[ -f file ]判斷是否是普通文件,且存在
• [ -d file ] 判斷是否是目錄,且存在
• [ -e file ] 判斷文件或目錄是否存在
• [ -r file ] 判斷文件是否可讀
• [ -w file ] 判斷文件是否可寫
• [ -x file ] 判斷文件是否可執行

[root@docker shell]# cat if4.sh
#!/bin/bash
f="/tmp/chenxu"
if [ -f $f ]
then
echo $f "exist"
else
touch /tmp/chenxu
fi

或者

#!/bin/bash
[ -f /tmp/chenxu ]&&echo "file exist"

! -f 指不存在
[root@docker shell]# cat if6.sh
#!/bin/bash
f="/tmp/chenxu"
if [ ! -f $f ]
then
echo $f "not exist"
else
echo haha
fi

• if [ -z "$a" ] 這個表示當變量a的值爲空時會怎麼樣
• if [ -n "$a" ] 表示當變量a的值不爲空
• if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行時會怎麼樣
• if [ ! -e file ]; then 表示文件不存在時會怎麼樣
• if (($a<1)); then …等同於 if [ $a -lt 1 ]; then…
• [ ] 中不能使用<,>,==,!=,>=,<=這樣的符號

[root@docker shell]# cat if7.sh
#!/bin/bash
f=
if [ -z $f ]
then
echo "$f is null"
else
echo haha
fi

[root@docker shell]# cat if7.sh
#!/bin/bash
n='wc -l /tmp/lalala'
if [ $n -gt 100 ]
then
echo ">100"
else
echo haha
fi

#/bin/bash
if [ ! -f /tmp/lalal ]
then
echo "/tmp/lalal not exist."
exit
fi
n=wc -l /tmp/lalal
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo haha
fi

變量若等於命令的結果,使用1旁邊那個鍵來擴住命令。

-n 可以判斷文件和變量

[root@docker shell]# if [ -n if9.sh ];then echo ok;fi
ok
[root@docker shell]# b=a;if [ -n "$b" ]; then echo ok;fi
ok
[root@docker shell]#

判讀user1 是否存在
grep -w ‘user1’ /etc/passwd

[root@docker shell]# if grep -w 'user1' /etc/passwd;then echo "user1 exist";else useradd user1 -d /home/user1;fi
[root@docker shell]# if grep -w 'user1' /etc/passwd;then echo "user1 exist";else useradd user1 -d /home/user1;fi
user1:x:1000:1001::/home/user1:/bin/bash
user1 exist

注意這裏返回了grep的內容,如何不返回呢? 加上q參數

[root@docker shell]# if grep -qw 'user1' /etc/passwd;then echo "user1 exist";else useradd user1 -d /home/user1;fi
user1 exist
[root@docker shell]#

• 格式 case 變量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
•在case程序中,可以在條件中使用|,表示或的意思, 比如
2|3)
command
;;

[root@docker shell]# read -p "Please input a number: " n
Please input a number: 12345
[root@docker shell]# echo $n
12345

#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
n1=echo $n|sed 's/[0-9]//g' -----------
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi

case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number range is 0-100."
;;
esac

echo $?

exit 後跟的數據字就是 echo$?的值

[root@docker shell]# cat test.sh
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
[root@docker shell]# sh test.sh
Please input a number:
Please input a number.
[root@docker shell]# echo $?
1
[root@docker shell]# sh test.sh
Please input a number: 10
[root@docker shell]# echo $?
0

for循環

• 語法:for 變量名 in 條件; do …; done
• 案例1
#!/bin/bash
sum=0
for i in seq 1 100
do
sum=$[$sum+$i]
echo $i
done
echo $sum

[root@docker shell]# cat for.sh 求和shell,注意i 後面的是個命令結果,所以要用1旁邊那個鍵的來框命令
#!/bin/bash
sum=0
for i in seq 1 100
do
sum=$[$sum+$i]
done
echo $sum

#!/bin/bash
cd /etc/
for a in ls /etc/
do
if [ -d $a ]
then
ls -d $a
fi
done

[root@docker shell]# touch 1 2
[root@docker shell]# touch 3\ 4.txt
[root@docker shell]# ls
[root@docker shell]# ls
1 1.sh 2 3 4.tx

for i in ls ./;do echo $i;done

for 裏面將回車和空格作爲了分隔符

[root@docker shell]# for i in ls ./;do echo $i;done
1
1.sh
2
3
4.txt

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章