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
10、写一个脚本,打印2^n表;n等于一个用户输入的值;(不好意思,我调皮了)
11、写一个脚本,写这么几个函数:函数1、实现给定的两个数值的之和;函数2、取给定两个数值的最大公约数;函数3、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将通过交互式输入来提供。