Shell script Programming

Source: Internet
Author: User

1. Write the shell script, calculate 1-100 's and;

#!/bin/bashsum=0;for((i=0;i<=100$sum$i  ))}echo$sum

2) Write the shell script, ask to enter a number, and then calculate from 1 to the input number and, if the input number is less than 1, then re-enter until the correct number is entered;

#!/bin/bashNumsave=0Read-P"Please input your number:"Num while true; DoifTest$num -lt 0; Then        Read-P"Please again:"NumElse         Breakfi Done for((i=0; i<=$num(i++)) {numsave=$ ($numsave+$i))}Echo $numsave

3) write the shell program to backup and restore the given directory as required.
Function:
Select 1 to back up the input directory as:/tmp
Select 2 to enable recovery of the backup directory to the current directory
Select 3, Exit
Requirements are implemented with functions:
Backup (): Implementation of directory backups, generating. tar.gz files
Restore (): Implementing a directory Recovery
TestDir (): To determine the name of the input directory, if the directory name does not exist, give an error message.

#!/bin/bashDir= $#ls-ld./$dirBackup() {tar-cvf/tmp/$dir. Tar./$dir}Restore() {tar-xvf/tmp/$dir./$dir}TestDir(){ifTest!- D $dir; ThenEcho "This isn ' t dir"#exit 0fi}testdir while true; DoSelect optinch ' BackUp Dir ' ' Restore Dir ' ' Exit '; Do         Case $opt inch        ' BackUp Dir ')#there isn ' tbackup;;' Restore Dir ') restore;; *)Exit 0;;Esac  Done Done

Shell script Programming

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.