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

时间:2022-04-10 15:46:23

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
)./usb;;
)./compression;;
)./changemod;;
)./changedate;;
)./findfile;;
)clear
echo " Thank you for using my program "
echo " Bye Bye "
sleep
while true
do
exit
done;;
*)echo " please input the right number "
sleep
clear;;
esac
done

usb程序

 #! /bin/sh
mntusb(){
clear
/sbin/fdisk -l|grep dev/sd
echo "please input the device name(like sdb1 or else)which is shown above"
read r
mount dev/$r/mnt
echo "if no error warn then"
echo "successfully! The USB has been mounted in /mnt document"
sleep
}
umntusb(){
clear
umount /mnt
echo "if no error warn then umount successfully!"
sleep
}
listusb(){
clear
ls -la/mnt
}
cpdisktousb(){
clear
echo "please input the filename to be copied in current directory"
read FILE1
cp $FILE1 /mnt
echo "if no error warn the copy successfully!"
sleep
}
cpusbtodisk(){
clear
echo "please input the filename to be copied in usb"
echo "PS:the file will be copied in the current directory"
read FILE2
cp /mnt/ $FILE2
echo "if no error warn then copy successfully"
sleep
}
rmusb(){
clear
echo "input the file you want remove in the diretory"
read FILE3
rm -v /mnt/$FILE3
echo "if no error warn then remove successfully!"
sleep
}
back(){
clear
exit
}
while true
do
clear
echo "================================================================"
echo "*** USB FUNCTIONS ********"
echo " ------------- "
echo " 1-mount USB "
echo " 2-umount USB "
echo " 3-list USB's files' information "
echo " 4-copy current directory's file in disk to USB "
echo " 5-copy USB's file to current diretory in disk "
echo " 6-remove the file in USB "
echo " 0-back "
echo "================================================================"
echo "input the number to choose the function "
echo "PS: If you want to do something to USB,please mount first "
read choice
case $choice
)mntusb;;
)umntusb;;
)listusb;;
)cpdisktousb;;
)cpusbtodisk;;
)rmusb;;
)back;;
*)echo "please input the right number!(press any key to continue)"
read c;
clear;;
esac
done

compression程序

 #! /bin/bash
replace(){
echo "please input the file's absolute way including the file's name"
read way1
gzip $ way1
echo "if no error warn then compress successfully!"
sleep
}
pack(){
echo "please input the directory's absolute way!"
read way2
way8=$way2.tar.gz
tar -zcvf $way8 $way2
echo "ignore the warn"
echo "if no error warn then operate successfully!"
sleep
}
back2(){
clear
./compression
}
filegzip(){
while true
clear
do
echo "============================================================"
echo " 1-Replace the original file with compressed file "
echo " 2-packing and compress directory "
echo " 0-back "
echo "============================================================"
echo "input the right number to choose the function "
read num1
case $num1 in
)replace;;
)pack;;
)back2;;
*)echo "please input the right order!"
sleep
clear;;
esac
done
}
fileunzip(){
echo "please input the file's absolute way including the file's name"
read way3
gzip-d $way3
echo "if no error warn then decompress successfully! "
sleep
}
back(){
clear
exit
}
while true
clear
do
echo "============================================================="
echo " COMPRESS FUNCTION "
echo " 1-Compress file "
echo " 2-Decompress file "
echo " 0-back "
echo "============================================================="
echo "please input the number to choose the function "
read choice2
case $choice2 in
)filegzip;;
)fileunzip;;
)back;;
*)echo "please input the right number!"
sleep
clear;;
esac
done

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

 #! /bin/bash
show(){
echo "input the file's absolute way including the file's name"
read way4
echo "the information of the file is list following "
ls -l $way4
sleep
}
change(){
echo "input the file's absolute way including the file's name"
echo "and input the file's properities you want change to,input like 700"
read way5 pro
echo "the formal properities of the file is like following"
ls -l $way5
chmod $pro $way5
echo "change successfully!"
echo "the changed file's properities is like following "
ls -l $way5
}
clear
while true
do
echo "========================================================="
echo " EDIT FUNCTION "
echo " 1-Show the properities of the file "
echo " 2-Change the properities of the file "
echo " 0-back "
echo "input the right number to choose function "
read num3
case $num3 in
)show;;
)change;;
)clear
exit;;
*)echo "please input the right number!"
sleep
clear;;
esac
done

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

 #! /bin/bash
show(){
echo "Now the date is: "
date
echo "Have a nice day!"
sleep
}
edit(){
echo "input the time you want to change to"
echo "Ex: 0312043307 represent for 2007-03-12-04:33"
read time2
date $ time2
echo "if no error warn then change date successfully"
sleep
}
while true
do
echo "======================================================"
echo " CHANGEDATE FUNCTION "
echo " 1-Show the time "
echo " 2-Edit the system time "
echo " 0-back "
echo "input the number to choose function "
read num3
case $num3 in
)show;;
)edit;;
)clear
exit;;
*)echo "please input the right number!"
sleep
clear;;
esac
done

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

 #! /bin/bash
schcom(){
clear
echo "input the complele file's name you want to search"
read file9
echo "input the way you want to search in(like /root)"
read dir9
find $dir9 -name "$ file9" -print
echo "press Enter to continue"
read c
}
schpar(){
echo "input the part file's name you remember"
read file7
file6=*$file7*
echo "input the way which you want to search in:(like /root)"
read dir7
find $dir7 -name "$ file6" -print
echo "the above way is the file's way you want to search!"
echo "please Enter to continue"
read c
}
while true
do
clear
echo "================================================================"
echo " SEARCH FUNCTION "
echo " 1-Search file if you remember the complete file's name "
echo " 2-Search file if you remember the part of file's name "
echo " 0-back "
echo "================================================================"
echo "please input the number to choose function "
read num5
case $num5 in
)schcom;;
)schpar;;
)clear
exit;;
*)echo "please input the right number"
sleep
clear;;
esac
done