Shell Script编程——USB挂载/复制文件/查找文件/压缩文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell Script编程——USB挂载/复制文件/查找文件/压缩文件相关的知识,希望对你有一定的参考价值。

PS:$引用变量的时候不要加空格。用了case,while的结构。

main文件

#!/bin/bash
chmod a+x changedate
chmod a+x changemod
chmod a+x usb
chmod a+x findfile
chmod a+x compression

clear
while true
do
  echo "welcome to my program"
  echo "wish you have a good day"
  echo ""
  echo "========================================================"
  echo "**              ALL       FUNCTION                    **"
  echo "                1-Usb function                          "
  echo "                2-Compression functions                 "
  echo "                3-Edit files‘properties                 "
  echo "                4-Time                                  "
  echo "                5-Search                                "
  echo "                0-Exit                                  "
  echo "========================================================"
  echo "please input the number to choose the function:         "
read num
  case $num in
    1)./usb;;
    2)./compression;;
    3)./changemod;;
    4)./changedate;;
    5)./findfile;;
    0)clear
    echo "      Thank you for using my program                  "
    echo "      Bye Bye                                         "
    sleep 1
    while true
    do
      exit
    done;;
    *)echo "    please input the right number                   "
    sleep 1
    clear;;
  esac
done

usb程序

 1 #! /bin/sh
 2 mntusb(){
 3   clear
 4   /sbin/fdisk -l|grep dev/sd
 5   echo "please input the device name(like sdb1 or else)which is shown above"
 6   read r
 7   mount dev/$r/mnt
 8   echo "if no error warn then"
 9   echo "successfully! The USB has been mounted in /mnt document"
10   sleep 1
11 }
12 umntusb(){
13   clear
14   umount /mnt
15   echo "if no error warn then umount successfully!"
16   sleep 1
17 }
18 listusb(){
19   clear
20   ls -la/mnt
21 }
22 cpdisktousb(){
23   clear
24   echo "please input the filename to be copied in current directory"
25   read FILE1
26   cp $FILE1 /mnt
27   echo "if no error warn the copy successfully!"
28   sleep 1
29 }
30 cpusbtodisk(){
31   clear
32   echo "please input the filename to be copied in usb"
33   echo "PS:the file will be copied in the current directory"
34   read FILE2
35   cp /mnt/ $FILE2
36   echo "if no error warn then copy successfully"
37   sleep 1
38 }
39 rmusb(){
40  clear
41  echo "input the file you want remove in the diretory"
42  read FILE3
43  rm -v /mnt/$FILE3
44  echo "if no error warn then remove successfully!"
45  sleep 1
46 }
47 back(){
48  clear
49  exit
50 }
51 while true
52  do 
53   clear
54   echo "================================================================"
55   echo "***        USB FUNCTIONS                ********"
56   echo "        -------------                    "
57   echo "        1-mount USB                    "
58   echo "        2-umount USB                    "
59   echo "        3-list USB‘s files‘ information            "
60   echo "        4-copy current directory‘s file in disk to USB    "
61   echo "        5-copy USB‘s file to current diretory in disk    "
62   echo "        6-remove the file in USB            "
63   echo "         0-back                        "
64   echo "================================================================"
65   echo "input the number to choose the function                "
66   echo "PS: If you want to do something to USB,please mount first    "
67   read choice
68   case $choice
69     1)mntusb;;
70     2)umntusb;;
71     3)listusb;;
72     4)cpdisktousb;;
73     5)cpusbtodisk;;
74     6)rmusb;;
75     0)back;;
76     *)echo "please input the right number!(press any key to continue)"
77     read c;
78         clear;;
79   esac
80 done
81 
82   

compression程序

 1 #! /bin/bash
 2 replace(){
 3  echo "please input the file‘s absolute way including the file‘s name"
 4  read way1
 5  gzip $ way1
 6  echo "if no error warn then compress successfully!"
 7  sleep 2
 8 }
 9 pack(){
10  echo "please input the directory‘s absolute way!"
11  read way2
12  way8=$way2.tar.gz
13  tar -zcvf $way8 $way2
14  echo "ignore the warn"
15  echo "if no error warn then operate successfully!"
16  sleep 3
17 }
18 back2(){
19  clear
20  ./compression
21 }
22 filegzip(){
23 while true
24 clear
25 do
26  echo "============================================================"
27  echo "        1-Replace the original file with compressed file   "
28  echo "        2-packing and compress directory           "
29  echo "        0-back                           "
30  echo "============================================================"
31  echo "input the right number to choose the function           "
32  read num1
33  case $num1 in
34  1)replace;;
35  2)pack;;
36  0)back2;;
37  *)echo "please input the right order!"
38    sleep 1
39    clear;;
40  esac
41 done
42 }
43 fileunzip(){
44  echo "please input the file‘s absolute way including the file‘s name"
45  read way3
46  gzip-d $way3
47  echo "if no error warn then decompress successfully!             "
48  sleep 1
49 }
50 back(){
51  clear
52  exit
53 }
54 while true
55 clear
56 do 
57   echo "============================================================="
58   echo "        COMPRESS FUNCTION                 "
59   echo "        1-Compress file                     "
60   echo "        2-Decompress file                 "
61   echo "        0-back                         "
62   echo "============================================================="
63   echo "please input the number to choose the function                 "
64   read choice2
65   case $choice2 in
66     1)filegzip;;
67     2)fileunzip;;
68     0)back;;
69     *)echo "please input the right number!"
70        sleep 2
71        clear;;
72   esac
73 done

changemod程序——改变文件的读写执行权限

 1 #! /bin/bash
 2 show(){
 3  echo "input the file‘s absolute way including the file‘s name"
 4  read way4
 5  echo "the information of the file is list following          "
 6  ls -l $way4
 7  sleep 1
 8 }
 9 change(){
10  echo "input the file‘s absolute way including the file‘s name"
11  echo "and input the file‘s properities you want change to,input like 700"
12  read way5 pro
13  echo "the formal properities of the file is like following"
14  ls -l $way5
15  chmod $pro $way5
16  echo "change successfully!"
17  echo "the changed file‘s properities is like following   "
18  ls -l $way5
19 }
20 clear
21 while true
22 do
23  echo "=========================================================" 
24  echo "        EDIT FUNCTION                    "
25  echo "        1-Show the properities of the file        "
26  echo "        2-Change the properities of the file           "
27  echo "        0-back                        "
28  echo "input the right number to choose function         "
29  read num3
30  case $num3 in
31    1)show;;
32    2)change;;
33    0)clear
34      exit;;
35    *)echo "please input the right number!"
36      sleep 1
37      clear;;
38  esac
39 done 

changedate程序——显示时间、修改系统时间

 1 #! /bin/bash
 2 show(){
 3  echo "Now the date is: "
 4  date
 5  echo "Have a nice day!"
 6  sleep 1
 7 }
 8 edit(){
 9  echo "input the time you want to change to"
10  echo "Ex: 0312043307 represent for 2007-03-12-04:33"
11  read time2
12  date $ time2
13  echo "if no error warn then change date successfully"
14  sleep 1
15 }
16 while true
17 do 
18  echo "======================================================"
19  echo "        CHANGEDATE FUNCTION                 "
20  echo "        1-Show the time                     "
21  echo "        2-Edit the system time                 "
22  echo "        0-back                              "
23  echo "input the number to choose function             "
24  read num3
25  case $num3 in 
26    1)show;;
27    2)edit;;
28    0)clear
29      exit;;
30    *)echo "please input the right number!"
31      sleep 1
32      clear;;
33   esac
34 done

findfile程序——查找文件(给出完整名称,部分名称,但是要给出绝对路径)

 1 #! /bin/bash
 2 schcom(){
 3  clear
 4  echo "input the complele file‘s name you want to search"
 5  read file9
 6  echo "input the way you want to search in(like /root)"
 7  read dir9
 8  find $dir9 -name "$ file9" -print
 9  echo "press Enter to continue"
10  read c
11 }
12 schpar(){
13  echo "input the part file‘s name you remember"
14  read file7
15  file6=*$file7*
16  echo "input the way which you want to search in:(like /root)"
17  read dir7
18  find $dir7 -name "$ file6" -print
19  echo "the above way is the file‘s way you want to search!"
20  echo "please Enter to continue"
21  read c
22 }
23 while true
24 do
25  clear
26  echo "================================================================"
27  echo "            SEARCH    FUNCTION                   "
28  echo "        1-Search file if you remember the complete file‘s name "
29  echo "        2-Search file if you remember the part of file‘s name  "
30  echo "        0-back                               "
31  echo "================================================================"
32  echo "please input the number to choose function               "
33  read num5
34  case $num5 in
35   1)schcom;;
36   2)schpar;;
37   0)clear
38     exit;;
39   *)echo "please input the right number"
40     sleep 1
41     clear;;
42  esac
43 done
44 
45   

 

以上是关于Shell Script编程——USB挂载/复制文件/查找文件/压缩文件的主要内容,如果未能解决你的问题,请参考以下文章

arch挂载U盘后没有网络了

shell编程笔记2--nfs挂载

shell编程:自动格式化硬盘并挂载

手机无法进入adb shell

如何将参数传递给 shell 脚本? [复制]

stm32如何将4G模块挂载成网卡