Shell Script Programming

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.