Linux操作系统下的shell程序设计

使用shell编写一个菜单,可以实现列出以下内容:

(1)查看目录

(2)切换目录

(3)创建文件

(4)编辑文件

(5)复制文件

(6)打包文件

(7)删除文件

(8)退出

until
		echo "*************"
        echo "1.查看目录"
		echo "2.切换目录"
		echo "3.创建文件"
		echo "4.编辑文件"
		echo "5.复制文件"
		echo "6.打包文件"
        echo "7.删除文件"
        echo "8.退出"
        echo "************"
        echo "请输入对应功能的序号:"
		read input
                test $input = 9 
                do
			case $input in
			1)echo "enter target directory:"
                              read dir
                              if [ -x $dir ]
                              then
                                  echo "The query is successful. The contents are as follows:"
                                  echo "-------------------------------------------------------"
                                  ls $dir
                              else
                                  echo "Directory is not available!"
                              fi;;
			2)echo "enter target directory:"
		   	      read dir
                              if [ -x $dir ]
                              then 
                                  cd $dir
                                  echo "Switching succeeded!"
                                  echo "The current directory is $dir"
			      else
                                  echo "Directory is not available"
                              fi;;
			3)echo "enter a file name"
			       read file
                               if [ ! -f $file ]   
			       then 
                                   touch $file
                                   echo "Created successfully!"
                               else 
                                   echo "File already exist!!"
                               fi;;
			4)echo "enter a file name:"
					read file
                                       if [ -w $file ]
				       then
                                          vi $file
                                       else
                                          echo "File is not writable!!"
                                       fi;;
                        5)echo "enter a file name:"
                                echo "当前目录文件有"
                                echo "******************************************"
                                ls
                                read file
                                if [ -f $file ]
                                then
                                    echo "输入想要复制文件的路径:"
                                    read dir
                                    if [ -x $dir ]
                                    then
                                        cp $file $dir
                                        echo "Copy succeeded!!"
                                    else
                                        echo "文件的路径不存在!!"
                                    fi
                                else
                                    echo "file is not copy"
                                fi;;
                        6)echo "enter a file name(form:file.tar file):"
                                echo "当前目录文件有"
                                echo "******************************************"
                              ls
                              read file
                              if [ -f $file ]
                              then 
                                  tar -cvf $file.tar $file
                                  echo "Packaging succeeded!!"
                              else
                                  echo "without permission"
                              fi;;
			7)echo "enter a file name"
                                echo "当前目录文件有"
                                echo "******************************************"
                                ls
				read file
                                if [ -f $file ]
                                then 
                                     echo "Are you sure you want to delete?(yes or no)"
                                       read input
                                       if [ $input == yes ]
				       then 
                                           rm -f $file
                                           echo "Delete success!"
                                       else 
                                           echo "Delete failed!"
                                       fi
                                else 
                                     echo "File does not exist" 
                                     echo "or Delete without permission!!"
                                 fi;;
			8)echo "Are you sure you want to quit?(yes or no)"
                              read input
                              if [ $input == yes ]
			      then 
                                  echo "Thanks for your choice!!"
                                  exit
                              else
                                  echo "Continue execution!"
                                  continue
                              fi;;
                       esac
		done

主要使用case...... done语句

shell常用的文件判断运算符如下:

-e 文件是否存在

-f  文件是否是普通文件(不是目录、设备文件、链接文件)

-s  表示文件大小不为0

-d 表示文件是否是目录

-b 表示是块设备(光驱、软盘等)

-c  表示是字符设备(键盘、声卡等)

-p 表示是管道

-h 表示是符号链接

-S 表示是否是socket

-r、-w、-x表示文件是否有可读、可写、可执行权限(指运行这个测试命令的用户)

f1 -nt f2      f1是否比f2新(new than)

f1 -ot f2      f1是否比f2旧(old than)

f1 -ef f2      f1和f2是否是相同文件的硬链接

       使用!时表示上述结果取反

运行结果如下

(1)查看目录

(2)切换目录

  

(3)创建文件

(4)编辑文件

 

(5)复制文件

(6)打包文件

(7)删除文件

(8)退出

 

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值