Linux Shell script instance 1

Source: Internet
Author: User

1. Write a script to calculate the factorial of 10 in a loop.

#! /Bin/sh </P> <p> factorial = 1 </P> <p> for a in 'seq 1 10' </P> <p> DO </P> <p> factorial = 'expr $ factorial/* $ a' </P> <p> done </P> <p> echo "10! = $ Factorial "<br/>

 

2. Write a script. After the script is executed, print a row with the prompt "Please input a number:". Ask the user to enter a value, then print the value, and then ask the user to enter a value again. Until the user enters "end" to stop.

#! /Bin/sh </P> <p> unset var </P> <p> while ["$ Var "! = "End"] </P> <p> DO </P> <p> echo-n "Please input a number: "</P> <p> Read var </P> <p> If [" $ Var "=" end "] </P> <p> then </P> <p> Break </P> <p> fi </P> <p> echo "Var is $ Var" </P> <p> done </P> <p>

 

3. Write a script and use the loop and continue keywords to calculate the sum of the three divisible numbers within 100

#! /Bin/sh </P> <p> sum = 0 </P> <p> for a in 'seq 1 100' </P> <p> DO </P> <p> If ['expr $ A % 3'-Ne 0] </P> <p> then </P> <p> continue </P> <p> FI </P> <p> echo $ A </P> <p> sum = 'expr $ sum + $ a' </P> <p> done </P> <p> echo "sum = $ sum" <br/>

 

4. A function uses shift to calculate the product of all parameters. Assume that all parameters are integers.

#! /Bin/sh </P> <p> result = 1 </P> <p> while [$ #-GT 0] </P> <p> DO </P> <p> result = 'expr $ result/* $ 1' </P> <p> shift </P> <p> done </P> <p> echo $ result </P> <p>

 

5.write a script, you can upload the file name, and call the tar.gzor tar.bz2 file with the specified parameter.

#! /Bin/sh </P> <p> case ${1 ##*. tar .} in </P> <p> bz2) </P> <p> tar jxvf $1 </P> <p>; </P> <p> GZ) </P> <p> tar zxvf $1 </P> <p >;</P> <p> *) </P> <p> echo "Wrong file type" </P> <p> esac </P> <p>

 

6. Write a script to help you query RPM information. This script first prompts the user to select the query basis, such as the file name, package name, and all. Then, the user is prompted to select the query information, such as the package name, all files contained in the package, and package information. Then, ask if you want to continue the query.

#! /Bin/sh </P> <p> RPM =/bin/RPM </P> <p> option = "-Q" </P> <p> while true </P> <p> DO </P> <p> echo "what to query? "</P> <p> select VaR in" all "" file "" package name "</P> <p> DO </P> <p> case $ VaR in </P> <p> All) </P> <p> option = $ option "A" </P> <p> Break </P> <p >;</P> <p> file) </P> <p> echo-n "Please input file name: "</P> <p> option = $ option" F "</P> <p> Read argument </P> <p> Break </P> <p>;; </P> <p> package/Name) </P> <p> echo-n "Please input package name: "</P> <p> Read argument </P> <p> Break </P> <p >;</P> <p> *) </P> <p> echo "Please choose 1-3 "</P> <p> ;; </P> <p> esac </P> <p> done </P> <p> echo "what do you want to know? "</P> <p> select VaR in" location "" info "" package name "</P> <p> DO </P> <p> case $ VaR in </P> <p> location) </P> <p> option = $ option "L" </P> <p> Break </P> <p >;</P> <p> info) </P> <p> option = $ option "I" </P> <p> Break </P> <p> ;; </P> <p> package/Name) </P> <p> Break </P> <p>; </P> <p> *) </P> <p> echo "Please choose between 1-3" </P> <p> ;; </P> <p> esac </P> <p> done </P> <p >$ {RPM} $ option $ argument </P> <p> echo" continue? [Yes/No] "</P> <p> read answer </P> <p> If [Answer =" no "] </P> <p> then </ p> <p> Break </P> <p> fi

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.