Linux实验5(初学shell编程)

1. 设计如下一个菜单驱动程序,保存为: menu.sh

Use one of the following options:
P:To display current directory
S:To display the name of running file $0
D:To display today’s date and present time(如:2017-04-26 05:45:12)
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
菜单程序将根据用户输入的选择项给出相应信息,直到输入Q|q才退出程序,否则一直提示操作信息

要求对用户的输入忽略大小写,对于无效选项的输入给出相应提示。要求使用case
语句实现以上功能,输入相应的字母后应该执行相应的命令完成每项功能,如输入P或p,就执行pwd命令。

 1 #!/bin/bash
 2 while true
 3 do
 4 echo -n "Enter your option and hit:"
 5 read option
 6 case "$option" in
 7 [pP])
 8 		pwd
 9 ;;
10 [sS])
11 		echo $0
12 ;;
13 [dD])
14 		date "+%Y-%m-%d %H:%M:%S"
15 ;;
16 [lL])
17 		ls
18 ;;
19 [wW])
20 		whoami
21 ;;
22 [iI])
23 		ip addr
24 ;;
25 [qQ])
26 		exit 0
27 ;;
28 *)
29 echo "you can only input 'p s d l w i q!'"
30 ;;
31 esac
32 done
33 exit 1;

2. 编写一段bash shell程序,保存为:score.sh

根据键盘输入的学生成绩,显示相应的成绩登等级,
其中
60分以下为"Failed!",
60~69分为"Passed!",
70~79分为"Medium!",
80~89分为"Good!",
90~100为"Excellent!"。
如果输入超过100的分数,则显示错误分数提示。

如果输入负数,则退出程序,否则一直提示用户输入成绩

 1 #!/bin/bash
 2 while true
 3 do
 4         echo -n "please input a student score:"
 5         read score
 6         if [ "$score" -gt 100 ];
 7         then
 8                 echo "input error!"
 9         elif [ "$score" -ge 90 ];
10         then
11                 echo "Excellent!"
12         elif [ "$score" -ge 80 ];
13         then
14                 echo "Good!"
15         elif [ "$score" -ge 70 ];
16         then
17                 echo "Medium!"
18         elif [ "$score" -ge 60 ];
19         then
20                 echo "Passed!"
21         elif [ "$score" -ge 0 ];
22         then
23                 echo "Failed!"
24         else
25                 exit 0
26         fi
27 done

3. 编写一段bash shell程序,保存为file.sh

判断用户输入是否为有效目录路径。如果不是,提示该路径不是目录路径。如果是,则依次输出该目录下的所有内容的 “文件路径_文件类型符号”形式。
直到用户输入q退出,否则一直提示用户输入。
例如:
/etc_d
/etc/passwd_-

#!/bin/bash
while true
do
        echo -n "please input a path:"
        read path
        if [ -d $path ];then
                for fileName in ${path}/*
                do
                if [ -d $fileName ];then
                        fileType="d"
                else
                        fileType=`ls -l $fileName|cut -c1`
                fi
                echo "${fileName}_${fileType}"
                done
        elif [ $path = "q" ];then
                exit 0
        else
                echo "the input is a valid directory path!"
        fi
done

一些问题

代码copy操作

linux里面
vim在一般模式下输入ggyG
gg:到文件头
y:复制
G:到文件尾
从secureCRT复制到windows
鼠标选中,右键|ctrl + c|在secureCRT工具栏选择粘贴

vim里的文本替换

:%s/搜索字符/被替换字符/g

%s表示进行字符替换
g表示全局,不加g表示当前行,不加/g默认全局

vim里搜索之后取消高亮

:noh

shell里面的if语句

if condition;then
     符合该条件执行的语句
elif condition;then
     符合该条件执行的语句
else
     符合该条件执行的语句
fi

注:
1.condition后面要加’;’
2.当condition可用test或者[ ]。
用[ ]表示的时候,应该是’[空格’和’空格]’。记得加上空格

vim里怎么把’[‘替换为’[ ’

:%s#\[#\[ 

其中’[‘需要利用’\'进行转义

查看文件类型

1.使用ll 或者ls-l,看第一个字符
2.使用file命令,如file jason.txt
3.使用stat命令,查看文件的详细信息。

Linux cut命令

  • -b :以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志。
  • -c :以字符为单位进行分割。
  • -d :自定义分隔符,默认为制表符。
  • -f :与-d一起使用,指定显示哪个区域。用逗号隔开
  • -n :取消分割多字节字符。仅和 -b 标志一起使用。如果字符的最后一个字节落在由 -b 标志的 List 参数指示的 范围之内,该字符将被写出;否则,该字符将被排除

例如:

#第一个字符
cut -c1
#查看/etc/passwd的第1,2,3段内容
cat /etc/passwd | cut -d : -f1,2,3

shell里面的自定义变量

bash的变量于型的变量,型变量的特点是声明变量 不用声明型和可以存储不同型的值,使用非常灵活。

注:使用方式为 变量=值
’='号左右不能有空格

猜你喜欢

转载自blog.csdn.net/LittleSeedling/article/details/105800079