Shell Script Programming
1. Write a shell script to calculate the sum of 1-100;
#!/bin/bashsum=0;for((i=0;i<=100;i++)){sum=$(( $sum + $i ))}echo $sum
2) Write a shell script that requires entering a number and then calculating the sum from 1 to the input number. If the input number is smaller than 1, enter the number again, until the correct number is entered;
#!/bin/bashnumsave=0read -p "please input your number:" numwhile true;doif test $num -lt 0;then read -p "please again:" numelse breakfidonefor((i=0;i<=$num;i++)){ numsave=$(($numsave+$i))}echo $numsave
3) Write a shell program to back up and restore the specified directory as required.
Function:
Select 1 to back up the input directory. The backup directory is/tmp.
Select 2 to restore the Backup Directory to the current directory.
Select 3 and exit
Function implementation is required:
Backup (): generate the .tar.gz File
Restore (): Directory recovery
Testdir (): determines the name of the input directory. If the directory name does not exist, an error is returned.
#!/bin/bashdir=$1#ls -ld ./$dirbackup(){ tar -cvf /tmp/$dir.tar ./$dir}restore(){ tar -xvf /tmp/$dir ./$dir}testdir(){ if test ! -d $dir;thenecho "this isn't dir"#exit 0fi}testdirwhile true;doselect opt in 'BackUp Dir' 'Restore Dir' 'Exit' ;do case $opt in 'BackUp Dir')#there isn't number backup ;; 'Restore Dir') restore ;; *) exit 0 ;; esac donedone