歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> SHELL編程

使用shell腳本實現USB設備的加載與文件復制

在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. echo "Welcome to USB"
  4. echo "Do you want load USB(Y/N)"
  5. read ANS
  6. if[$ANS="Y" -o $ANS = "y"];
  7. then mount -t vfat /dev/sdc1 /mnt/usb
  8. echo "Do you want to copy files to /root(y/n)?"
  9. read ANS
  10. while[$ANS="Y" -o $ANS = "y"]
  11. do
  12. ls -lha /mnt/usb
  13. echo "type the filename you want to copy"
  14. read FILE
  15. cp /mnt/usb/"$FILE" /root
  16. if[ $? -eq 0];then
  17. echo "Finished"
  18. else
  19. echo "Error"
  20. fi
  21. echo "any other files(Y/N)"
  22. read ANS
  23. done
  24. fi
  25. echo "Do you want to copy files to USB(y/n)"
  26. read ANS
  27. while[$ANS="Y" -o $ANS = "y"]
  28. do
  29. ls -lh /root
  30. echo "type the filename you want to copy"
  31. read FILE
  32. cp /root/"$FILE" /mnt/usb
  33. if[ $? -eq 0];then
  34. echo "Finished"
  35. else
  36. echo "Error"
  37. fi
  38. echo "any other files(Y/N)"
  39. read ANS
  40. done
  41. echo "Do you want to umount?(y/n)"
  42. read ANS
  43. if[$ANS="Y" -o $ANS = "y"];then
  44. umount /mnt/usb
  45. else
  46. echo "umount error"
  47. fi
  48. echo "GoodBye!!"
Copyright © Linux教程網 All Rights Reserved