在Linux操作系统中,如果插入一个USB设备,需要用mount挂载命令才能实现这个设备的加载,下面写一个USB设备挂载与文件复制的Shell程序,程序需求: 1、运行时,...">  在Linux操作系统中,如果插入一个USB设备,需要用mount挂载命令才能实现这个设备的加载,下面写一个USB设备挂载与文件复制的Shell程序,程序需求: 1、运行时,..." />  在Linux操作系统中,如果插入一个USB设备,需要用mount挂载命令才能实现这个设备的加载,下面写一个USB设备挂载与文件复制的Shell程序,程序需求: 1、运行时,..." />
用户登录
用户注册

分享至

使用shell脚本实现USB设备的加载与文件复制

  • 作者: meet-
  • 来源: 51数据库
  • 2020-08-27
editor-side-new">

 在Linux操作系统中,如果插入一个USB设备,需要用mount挂载命令才能实现这个设备的加载,下面写一个USB设备挂载与文件复制的Shell程序,程序需求:

1、运行时,提示用户输入“y”或者“Y”,确定是否挂载USB设备,U盘文件/dev/sdc1

  1. if[$ANS="Y" -o $ANS = "y"
  2.     then mount -t vfat /dev/sdc1 /mnt/usb 

2、确定是否复制文件到/root
最好用$?判断一下是否复制成功,$? -eq 0,表示复制成功

  1. while[$ANS="Y" -o $ANS = "y"
  2.     do 
  3.         ls -lha /mnt/usb 
  4.         echo "type the filename you want to copy" 
  5.         read FILE 
  6.         cp /mnt/usb/"$FILE" /root 

3、确定是否复制文件到USB设备中

  1. echo "Do you want to copy files to USB(y/n)" 
  2. read ANS 
  3. while[$ANS="Y" -o $ANS = "y"
  4.     do 
  5.         ls -lh /root 
  6.         echo "type the filename you want to copy" 
  7.         read FILE 
  8.         cp /root/"$FILE" /mnt/usb 
  9.         if[ $? -eq 0];then 
  10.             echo "Finished" 
  11.             else 
  12.             echo "Error" 
  13.         fi 
  14.         echo "any other files(Y/N)" 
  15.         read ANS 
  16.     done 

完整的脚本:

  1. #!/bin/bash 
  2. #autousb 
  3.  
  4. echo "Welcome to USB" 
  5. echo "Do you want load USB(Y/N)"     
  6. read ANS 
  7.  
  8. if[$ANS="Y" -o $ANS = "y"]; 
  9.     then mount -t vfat /dev/sdc1 /mnt/usb 
  10.     echo "Do you want to copy files to /root(y/n)?" 
  11.     read ANS 
  12.     while[$ANS="Y" -o $ANS = "y"
  13.     do 
  14.         ls -lha /mnt/usb 
  15.         echo "type the filename you want to copy" 
  16.         read FILE 
  17.         cp /mnt/usb/"$FILE" /root 
  18.         if[ $? -eq 0];then 
  19.             echo "Finished" 
  20.             else 
  21.             echo "Error" 
  22.         fi 
  23.     echo "any other files(Y/N)" 
  24.     read ANS 
  25.     done 
  26. fi 
  27.  
  28. echo "Do you want to copy files to USB(y/n)" 
  29. read ANS 
  30. while[$ANS="Y" -o $ANS = "y"
  31.     do 
  32.         ls -lh /root 
  33.         echo "type the filename you want to copy" 
  34.         read FILE 
  35.         cp /root/"$FILE" /mnt/usb 
  36.         if[ $? -eq 0];then 
  37.             echo "Finished" 
  38.             else 
  39.             echo "Error" 
  40.         fi 
  41.         echo "any other files(Y/N)" 
  42.         read ANS 
  43.     done 
  44.  
  45. echo "Do you want to umount?(y/n)" 
  46. read ANS 
  47.  
  48.  
  49. if[$ANS="Y" -o $ANS = "y"];then 
  50.         umount /mnt/usb 
  51. else 
  52.     echo "umount error" 
  53. fi 
  54. echo "GoodBye!!" 

软件
前端设计
程序设计
Java相关