1、描述shell程序的運行原理;
shell既是命令語言、程序設計語言也是命令解釋程序。
shell腳本通常是以#!起始的文本文件,如“#!/bin/bash”,這行被稱爲shebang,指定了解釋此腳本shell的路徑,執行腳本時系統會調用這個shell來運行此腳本。字符#指明註釋的開始。註釋部分以#爲起始,一直延續到行尾,註釋行通常用於爲代碼提供註釋信息,或者用於暫停執行某行代碼,會被解釋器忽略。
shell腳本是把命令堆砌在一起,shell通過詞法分析,語法分析,語義分析,按順序、選擇或循環執行腳本中的命令。腳本運行結束,此shell進程也即終止
2、總結shell編程中所涉及到的所有知識點(如:變量、語法、命令狀態等等等,要帶圖的喲);
變量、變量運算及條件測試
條件判斷語句if、case及read命令
循環語句for,while,until
函數、死循環、模塊話編程
3、總結課程所講的所有循環語句、條件判斷的使用方法及其相關示例;(if (jpg|png is not exist);echo ”You say a XX“)
循環語句for,while,until
變量、變量運算及條件測試
4、寫一個腳本:如果某路徑不存在,則將其創建爲目錄;否則顯示其存在,並顯示內容類型;(不要懷疑,就是這麼簡單)
[root@liaodijin ~]# cat test.sh
#!/bin/bash
#
read -t 10 -p "Please input a file path: " path
if [ -z "$path" ];then
echo -e "\n\033[31mError:\033[0mplease input a file path"
exit 1
elif [ ! -e "$path" ];then
mkdir -p $path $>/dev/null
echo "your input $path is not exist"
elif [ -f "$path" ];then
echo "$path is a general file"
echo -e "\033[31mThe $path:\033[0m"
cat $path
elif [ -d "$path" ];then
echo "$path is a directory"
echo -e "\033[31mThe $path:\033[0m"
ls $path
else echo "$path unknown type"
fi
測試結果:
[root@liaodijin ~]# bash test.sh
Please input a file path: /etc/fstab
/etc/fstab is a general file
The /etc/fstab:
#
# /etc/fstab
# Created by anaconda on Thu Sun 19 22:48:50 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=46c42e9f-f101-4209-9590-afa295415eaf /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_data /data ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@liaodijin ~]# bash test.sh
Please input a file path: /tmp
/tmp is a directory
The /tmp:
ks-script-utpPS_ yum.log
ks-script-utpPS_.log yum_save_tx-2015-09-15-20-24RcJDyn.yumtx
shell yum_save_tx-2015-09-16-20-29ihcHuJ.yumtx
test.sh yum_save_tx-2015-09-16-20-30ZOEQ7w.yumtx
test.sh yum_save_tx-2015-09-16-20-32anadvE.yumtx
[root@liaodijin ~]# bash test.sh
Please input a file path: xx
your input xx is not exist
[root@liaodijin ~]# ls
~ cc shell test3.sh xx
$ init.sh test1.sh test3.sh.orig
aa install.log test1.sh.orig test.sh
anaconda-ks.cfg install.log.syslog test.sh test.sh.orig
bb qq test.sh.orig trash.sh
[root@liaodijin ~]# bash test.sh
Please input a file path: /dev/null
/dev/null unknown type
[root@liaodijin ~]# bash test.sh
Please input a file path:
Error:please input a file path
[root@liaodijin ~]# bash test.sh
Please input a file path: aa bb cc
your input aa bb cc is not exist
5、寫一個腳本完成如下功能:判斷給定的兩個數值,哪個大哪個小,給定數值的方法:腳本參數,命令交互(使用read,依然如此簡單)
[root@test shell]# cat 6.sh
#!/bin/bash
#
error(){
cat<<EOF
+--------------------------------------------------------+
| Function of the script is to compare two numbers size |
| Instructions:num_compare.sh num1 num2 |
| Example:num_compare.sh 11 22 |
+--------------------------------------------------------+
EOF
}
echo "$*"|grep '[[:alpha:]]' &>/dev/null && error && exit 1
if [ $# -gt 2 -o $# -le 1 ];then
error
exit 2
elif [ "$1" -gt "$2" ];then
echo -e "The max is $1\nThe min is $2"
elif [ "$1" -lt "$2" ];then
echo -e "The max is $2\nThe min is $1"
elif [ "$1" -eq "$2" ];then
echo "$1 and $2 as large as"
else error
fi
測試結果如下:
[root@test shell]# bash 6.sh
+--------------------------------------------------------+
| Function of the script is to compare two numbers size |
| Instructions:num_compare.sh num1 num2 |
| Example:num_compare.sh 11 22 |
+--------------------------------------------------------+
[root@test shell]# bash 6.sh a 1
+--------------------------------------------------------+
| Function of the script is to compare two numbers size |
| Instructions:num_compare.sh num1 num2 |
| Example:num_compare.sh 11 22 |
+--------------------------------------------------------+
[root@test shell]# bash 6.sh 1 a
+--------------------------------------------------------+
| Function of the script is to compare two numbers size |
| Instructions:num_compare.sh num1 num2 |
| Example:num_compare.sh 11 22 |
+--------------------------------------------------------+
[root@test shell]# bash 6.sh xy zx
+--------------------------------------------------------+
| Function of the script is to compare two numbers size |
| Instructions:num_compare.sh num1 num2 |
| Example:num_compare.sh 11 22 |
+--------------------------------------------------------+
[root@test shell]# bash 6.sh 12a 2b3
+--------------------------------------------------------+
| Function of the script is to compare two numbers size |
| Instructions:num_compare.sh num1 num2 |
| Example:num_compare.sh 11 22 |
+--------------------------------------------------------+
[root@test shell]# bash 6.sh 10 34
The max is 34
The min is 10
6、求100以內所有奇數之和(至少用3種方法。是的這是我們的作業^_^)
(1)、[root@liaodijin ~]# cat test4.sh
#!/bin/bash
#
y=0
for x in $(seq 1 2 100);do # for循環
y=$[$y+$x]
done
echo "The sum is $y"
[root@liaodijin ~]# bash test4.sh
The sum is 2500
(2)、[root@liaodijin ~]# cat test5.sh
#!/bin/bash
#
x=1
while [ "$x" -le 100 ];do # while循環
if [ $[$x%2] -eq 1 ];then
let y+=$x
fi
let x++
done
echo "The sum is $y"
[root@liaodijin ~]# bash test5.sh
The sum is 2500
(3)、[root@liaodijin ~]# cat test10.sh
#!/bin/bash
#
x=1
until [ $x -gt 100 ];do # until循環
if [ $[$x%2] -eq 1 ];then
let y+=$x
fi
let x++
done
echo "The sum is $y"
[root@liaodijin ~]# bash test10.sh
The sum is 2500
7、寫一個腳本實現如下功能:
(1) 傳遞兩個文本文件路徑給腳本;
(2) 顯示兩個文件中空白行數較多的文件及其空白行的個數;
(3) 顯示兩個文件中總行數較多的文件及其總行數;
[root@liaodijin ~]# cat test6.sh
#!/bin/bash
#
read -t 10 -p "Please enter two file path: " file1 file2 #發現一個問題,這裏我不輸入等10S後爲什麼不會結束,按ENTER鍵也不會結束
spaceline1=$(grep "^$" $file1|wc -l)
spaceline2=$(grep "^$" $file2|wc -l)
line1=$(cat $file1|wc -l)
line2=$(cat $file2|wc -l)
###############################################################
compare(){ # 這裏我們發現第2問和第3問的要求通過一個表達式只是參數不同就可以實現,所以我們這裏先定義一個函數,調用2次就可以實現,省得重複寫命令
if [ $a -gt $b ];then
echo -e " max $file1\n $file1 :$a"
elif [ $a -lt $b ];then
echo -e "max $file2\n $file2 :$b"
elif [ $a -eq $b ];then
echo -e "$file1 and $file2 as large as\n space line is $a"
else echo "Error please enter two file path!"
fi
}
##############################################################
a=$spaceline1
b=$spaceline2
echo "The space_line:"
compare
echo
a=$line1
b=$line2
echo "The line:"
compare
測試結果:
[root@liaodijin ~]# bash test6.sh
Please enter two file path: /root/aa /root/bb
The space_line:
max /root/aa
/root/aa :2
The line:
max /root/bb
/root/bb :11
[root@liaodijin ~]# cat aa
aaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaa
[root@liaodijin ~]# cat bb
bbbbbbbbbbbb
bbbbbbbbb
a
b
c
ccc
cccc
d
d
c
8、寫一個腳本
(1) 提示用戶輸入一個字符串;
(2) 判斷:
如果輸入的是quit,則退出腳本;
否則,(繼續等待用戶輸入並)顯示其輸入的字符串內容;
[root@liaodijin ~]# cat test8.sh
#!/bin/bash
#
read -t 10 -p "Please enter string: " var
case $var in
quit)
exit 1
;;
*)
echo "$var"
;;
esac
測試結果:
[root@liaodijin ~]# bash test8.sh
Please enter string:
[root@liaodijin ~]# bash test8.sh
Please enter string: nihaodaf
nihaodaf
[root@liaodijin ~]# bash test8.sh
Please enter string: quit
2、
[root@test shell]# cat 9.sh
#!/bin/bash
#
while true;do
read -p "Please enter string: " str
[ $str == quit ]&&break
echo $str
done
測試結果:
[root@test shell]# bash 9.sh
Please enter string: nihao
nihao
Please enter string: sb
sb
Please enter string: quit