banbanbai 发表于 2018-8-17 10:13:21

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

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

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

  


[*]if[$ANS="Y" -o $ANS = "y"]
[*]    then mount -t vfat /dev/sdc1 /mnt/usb
  

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


  


[*]while[$ANS="Y" -o $ANS = "y"]
[*]    do
[*]      ls -lha /mnt/usb
[*]      echo "type the filename you want to copy"
[*]      read FILE
[*]      cp /mnt/usb/"$FILE" /root
  

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

  

  


[*]echo "Do you want to copy files to USB(y/n)"
[*]read ANS
[*]while[$ANS="Y" -o $ANS = "y"]
[*]    do
[*]      ls -lh /root
[*]      echo "type the filename you want to copy"
[*]      read FILE
[*]      cp /root/"$FILE" /mnt/usb
[*]      if[ $? -eq 0];then
[*]            echo "Finished"
[*]            else
[*]            echo "Error"
[*]      fi
[*]      echo "any other files(Y/N)"
[*]      read ANS
[*]    done
  

  完整的脚本:
  


[*]#!/bin/bash
[*]#autousb
[*]
[*]echo "Welcome to USB"
[*]echo "Do you want load USB(Y/N)"
[*]read ANS
[*]
[*]if[$ANS="Y" -o $ANS = "y"];
[*]    then mount -t vfat /dev/sdc1 /mnt/usb
[*]    echo "Do you want to copy files to /root(y/n)?"
[*]    read ANS
[*]    while[$ANS="Y" -o $ANS = "y"]
[*]    do
[*]      ls -lha /mnt/usb
[*]      echo "type the filename you want to copy"
[*]      read FILE
[*]      cp /mnt/usb/"$FILE" /root
[*]      if[ $? -eq 0];then
[*]            echo "Finished"
[*]            else
[*]            echo "Error"
[*]      fi
[*]    echo "any other files(Y/N)"
[*]    read ANS
[*]    done
[*]fi
[*]
[*]echo "Do you want to copy files to USB(y/n)"
[*]read ANS
[*]while[$ANS="Y" -o $ANS = "y"]
[*]    do
[*]      ls -lh /root
[*]      echo "type the filename you want to copy"
[*]      read FILE
[*]      cp /root/"$FILE" /mnt/usb
[*]      if[ $? -eq 0];then
[*]            echo "Finished"
[*]            else
[*]            echo "Error"
[*]      fi
[*]      echo "any other files(Y/N)"
[*]      read ANS
[*]    done
[*]
[*]echo "Do you want to umount?(y/n)"
[*]read ANS
[*]
[*]
[*]if[$ANS="Y" -o $ANS = "y"];then
[*]      umount /mnt/usb
[*]else
[*]    echo "umount error"
[*]fi
[*]echo "GoodBye!!"


页: [1]
查看完整版本: 使用shell脚本实现USB设备的加载与文件复制