当前位置:   article > 正文

Linux shell 脚本实现u盘挂载及文件拷贝_linux usb插入脚本

linux usb插入脚本

Linux shell 脚本实现u盘挂载及文件拷贝

对于linux系统来说。使用mount命令(需管理员模式使用)来进行挂载硬盘

对于我的虚拟机来说。在插入u盘时,使用sudo fdisk -l命令设备显示在/dev/sdb1

硬盘分区不在该目录,需自行修改。

对于取消挂载使用umount命令

拷贝操作使用cp命令

第一个参数为出发点,第二个参数为目的地

使用循环菜单来进行功能选择

case类似与c中case1)表示var为1时进入。

完整代码如下:

#! /bin/bash
#huangrui 2022/9/20
flag=0
function menu(){
		echo "1. mount u "
		echo "2. umount u"
		echo "3. read u"
		echo "4. copy file to u"
		echo "5. copy file to system"
		echo "enter 6 to exit"

}

function mountU(){
	if [ $flag -eq 0 ];then		
		if [ ! -d "/mnt/usb" ];then
			sudo mkdir /mnt/usb
		fi	
		sudo mount /dev/sdb1 /mnt/usb
		flag=1
	else 
		echo -e " you have mounted \n"
	fi
}

function umountU(){
	sudo umount /mnt/usb
}

function readU(){
	cd /mnt/usb
	ls
        cd ..
}

function copyFileToU(){
	echo 'please choose your file which you want copy to U'
	read path
	echo 'please choose the copy destination'
	read destination
	cp $path /mnt/usb/${destination}S
}

function copyFileToSystem(){
	echo 'please choose your file which you want copy to system:'
	read path
	echo 'plase choose the copy destination:'
	read destination
	cp /mnt/usb/${path} $destination
}

function main(){
	while true
	do 
		menu
		read var 
		case ${var} in
			1)
				mountU
				;;
			2)
				umountU
				;;
			3)
				readU
				;;
			4)
				copyFileToU
				;;
			5)
				copyFileToSystem
				;;
			*)
				umountU
				exit 0
				;;
		esac
	done
}

main

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
2022/10/05修改,修复了无法从系统复制文件到u盘中的不存在文件夹
#! /bin/bash
#huangrui 2022/9/20
flag=0
function menu(){
		echo "1. mount u "
		echo "2. umount u"
		echo "3. read u"
		echo "4. copy file to u"
		echo "5. copy file to system"
		echo "enter 6 to exit"

}

function mountU(){
	if [ $flag -eq 0 ];then		
		if [ ! -d "/mnt/usb" ];then
			sudo mkdir /mnt/usb
		fi	
		sudo mount /dev/sdb1 /mnt/usb
		flag=1
	else 
		echo -e " you have mounted \n"
	fi
}

function umountU(){
	sudo umount /mnt/usb
}

function readU(){
	cd /mnt/usb
	ls
        cd ..
}

function copyFileToU(){
	echo 'please choose your file which you want copy to U'
	read path
	echo 'please choose the copy destination'
	read destination
	if [ ! -d "/mnt/usb/${destination}" ];then
			sudo mkdir /mnt/usb/${destination}
		fi
	cp $path /mnt/usb/${destination}
}

function copyFileToSystem(){
	echo 'please choose your file which you want copy to system:'
	read path
	echo 'plase choose the copy destination:'
	read destination
	cp /mnt/usb/${path} $destination
}

function main(){
	while true
	do 
		menu
		read var 
		case ${var} in
			1)
				mountU
				;;
			2)
				umountU
				;;
			3)
				readU
				;;
			4)
				copyFileToU
				;;
			5)
				copyFileToSystem
				;;
			*)
				umountU
				exit 0
				;;
		esac
	done
}

main




  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

注意,在使用时需要对脚本进行chmod +x 命令使脚本可以运行

并且,此脚本为bash脚本,若使用zsh需自行修改

使用vmware若u盘无法加载,可以在主页面进行usb控制器修改为usb3.1。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/83489
推荐阅读
相关标签
  

闽ICP备14008679号