File type: Normal file, directory file, socket file, device file, link file
determines whether a file is a symbolic link file. Yes, move it to the/etc directory; No, do not handle it .
1 #! /bin/bash 2 #filename: a.sh 3 filename= 4 echo "Input file name:" 5 read filename 6 if [-l $FILENAME] 7 then 8 MV $FILENAME/etc 9 fi
Design a shell program, give the user a prompt menu, according to the user's choice to do the corresponding action. The menu has the following 4 options:
(1) Start VI Editor
(2) Start X-window, enter the graphical interface
(3) starts the spool process and prints the specified file according to the entered file name.
(4) Prompt press the key combination CTRL + C to exit the program
The menu is re-displayed when the user's selection does not match the above menu item.
1 #! /bin/bash 2 #filename:a.sh 3 echo "please choose: (1-3, or ^c) " 4 5 echo " 1 vi " 6 echo "2 x-window" 7 echo "3 print-file" 8 echo "Ctrl+c quit" 9 10 read xyz 11 if [ $xyz = 1 ] 12 then 13 vi 14 exit 1 15 elif [ $xyz = 2 ] 16 then 17 startx 18 exit 1 19 elif [ $xyz = 3 ] 20 then 21 echo "//please enter filename" 22 read filename 23 lp $filename & 24 exit 1 25 else 26 echo "---------------------------" 27 /bin/sh a.sh 28 fi
- The
-
Designs a shell program that implements the menu function, which is to perform the appropriate action based on the user input. The menu is shown below.
(1) When the user enters start, the application is started in the background myfiles
(2) When the user enters stop, the Myfiles program that runs in the background is closed
(3) When the user enters status, View information about the Myfiles process
(4) Displays the Quit,thanks and exits the program when the user enters quit
to cycle through the menu when the user enters non-start, stop, status, quit. In this example, it is assumed that Myfiles is an executable program that remains in the current directory.
1 #! /bin/bash 2 #filename:a.sh 3 i=1 4 while [ "$i" -eq 1 ] 5 do 6 response= 7 echo "-----------------------------------------------" 8 echo "Please choice:" 9 echo "Input -- start -- start program myfiles. " 10 echo "Input -- stop --view status of myfiles process. " 11 echo "Input -- quit -- exit." 12 echo "-----------------------------------------------" 13 read RESPONSE 14 case $RESPONSE in 15 start) ./myfiles&;; 16 stop) killall myfiles;; 17 status) ps -aux | grep myfiles;; 18 quit) i=0 19 echo "Quit,thanks" 20 ;; 21 esac 22 done
Touch file, create file
Design a shell program to back up and compress all the files and subdirectories of the/etc directory on the 1st 0:00 each month. The backup file is stored in the/root/etcbak directory, the name of the backed up file is in the following format: YYMMDDD_etc.tar.gz, where yy represents the year, MM for the month, and DD for the day.
1 #! /bin/bash 2 #filename: a.sh 3 dirname= ' Ls/root | grep subbak ' 4 echo $DIRNAME 5 If [-s $DIRNAME]; Then 6 Mkdir/root/subbak 7 fi 8 Cd/root/subbak 9 yy= ' date+%y ' mm= ' date+%m ' one dd= ' date+%d ' backetc= $YY $mm$dd _etc.tar.gz tar zcvf $BACKETC/etc echo "File backup finished!"
1 #! /bin/bash 2 #filename: a.sh 3 dirname= ' Ls/root | grep subbak ' 4 echo $DIRNAME 5 If [-s $DIRNAME]; Then 6 Mkdir/root/subbak 7 fi 8 Cd/root/subbak 9 yy= ' date +%y ' mm= ' date +%m ' one dd= ' date +%d ' backetc= $YY $mm ${dd}_test.tar.gz tar zcvf $BACKETC test echo "File backup finished!"
Note: There must be a space between date and +, otherwise there is an error in execution; 15 lines, DD must be enclosed in {}, otherwise the $dd_test output will be empty.
In order to achieve the monthly 1st scheduled automatic backup, need to further write task timer. First, save the program a.sh to the/usr/bin directory, and then use the CRONTAB-E command to add a timed task, as shown below.
# Crontab-eselect an editor. To change later, run ' Select-editor '. 1./bin/ed 2. /bin/nano <----easiest 3. /usr/bin/vim.basicchoose 1-3 [2]:
Select 3, then enter 0 0 1 * */usr/bin/a.sh
Shell programming enables unified backup of multiple files or directories with similar characteristics at once
1 #! /bin/bash 2 #filename: a.sh 3 i=0 4 Find/-name Test > file 5 if [-d/home/si/test]; Then 6 echo "/home/si/test already exists." 7 Else 8 Mkdir/home/si/test 9 echo "/home/si/test is created." Ten fi and read line dirs= $LINE backup= "/home/si/test/backup${i}.tar.gz" TAR-ZCVF $BACKUP $DIRS i=$ (($i + 1)) Done<file
In order to implement automatic backups every 50 minutes, a task timer needs to be written further. First, save the program a.sh to the/usr/bin directory, and then use the CRONTAB-E command to add a timed task, as shown below.
0/50 * * * */usr/bin/a.sh
Design a shell program, automatically log on to the remote FTP server 192.168.20.122, automatically download the rc.local file under the/ETC/RC.D directory. Log on to the server using the user name teacher, password 654321, in binary transfer mode, and at the time of download with a hash display download progress.
Use some of the common FTP commands, such as bin, hash, get, bye and so on.
1 #! /bin/bash 2 #filename: a.sh 3 echo "Open 192.168.20.121 #建立到192.168.20.121 Connection 4 user teacher 654321 #用户名和密码 5 bin #采用 Binary transfer Mode 6 hash #进行hash, that is, symbol # indicates the progress of the transmission 7 CD/ETC/RC.D #切换到目录/etc/rc.d 8 Get rc.local #下载文件rc. Local 9 bye #结束本次ftp连接 10 "| Ftp-n #-n indicates that automatic logon is not allowed
-
Writes a shell program that backs up important resources of the system, including the following:
(1) System configuration file, which is all files in the/ETC/RC.D directory
(2) All configuration files in/etc directory, i.e.. conf files
(3) The system log file, that is, all files in the/var/log directory
Backup, transfers the backup file over FTP to the remote host 192.168.20.122, and the login FTP user name is teacher and the password is 654321. FTP connection using binary transfer mode, display upload progress and prompt display
1 #! /bin/bash 2 #filename: A.sh 3 mkdir -p /backup-sys 4 if [ $? -eq 0 ] ; then 5 tar zcvf /backup-sys/rc.tar.gz /etc/rc.d 6 tar zcvf / backup-sys/conf.tar.gz /etc/*.conf 7 tar zcvf /backup-sys/ Log.tar.gz /var/log 8 cd /backup-sys 9 if [ $? -eq 0 ]; then 10 echo "open 192.168.20.121 11 user teacher 654321 12 binary 13 prompt 14 hash 15 mput * 16 bye " | ftp -n 17 else 18 echo "could not into backup-sys directory!" 19 fi 20 else 21 echo "Could not create backup directory! "; 22 fi
In order to achieve regular automatic backup and file upload operation every Monday 0:00, the task timer needs to be further written. First save the program a.sh to the/usr/bin directory, and then use the CRONTAB-E command to add the scheduled task.
1 0 * * mon/usr/bin/a.sh