Chapter 2. Simple shell script exercises for shell scripts

Source: Internet
Author: User
Tags date1 integer numbers

Simple shell script exercises



Simple Example



  • Conversation Script: The variable content is determined by the user.

 

[[Email protected] Scripts] # vi sh02.sh #! /Bin/bash # program: # user inputs his first name and last name. program shows his full name. # History: #2005/08/23 vbirdfirst releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport pathread-P "Please input your first name:" firstname # prompt the user to enter read-P "Please input your last name: "lastname # prompt the user to enter echo-e" \ Nyour full name is: $ firstname $ lastname "# The result is output by the screen



  • Change with date: use date to create a file

 

[[Email protected] Scripts] # vi sh03.sh #! /Bin/bash # program: # program creates three files, which named by user's input # And date command. # History: #2005/08/23 vbirdfirst releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport path #1. ask the user to enter the file name and obtain the fileuser variable; echo-e "I will use 'touch' command to create 3 files. "# purely display information read-P" Please input your filename: "fileuser # Prompt user input #2. to prevent users from pressing enter at will, is there any configuration for the file name using the variable function? Filename =$ {fileuser:-"FILENAME"} # Start to determine whether there is a configuration file name #3. start to use the date command to obtain the required file name; date1 = $ (date -- date = '2 Days ago '+ % Y % m % d) # date2 = $ (date -- date = '1 days ago '+ % Y % m % d) # date of the previous day date3 =3 (date + % Y % m % d) # Today's date: file1 =1 {filename }$ {date1} # the bottom three rows are in the configuration file name file2 =$ {filename }$ {date2} file3 =3 {filename }$ {date3} #4. create a file name! Touch "$ file1" # Create File touch in the following three lines "$ file2" Touch "$ file3"



  • Numeric calculation: simple addition, subtraction, multiplication, division

"$ (Computational formula)": numerical calculation. Only integers are supported in BASH Shell by default.

[[email protected] scripts]# vi sh04.sh#!/bin/bash# Program:#User inputs 2 integer numbers; program will cross these two numbers.# History:# 2005/08/23VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHecho -e "You SHOULD input 2 numbers, I will cross them! \n"read -p "first number:  " firstnuread -p "second number: " secnutotal=$(($firstnu*$secnu))echo -e "\nThe result of $firstnu x $secnu is ==> $total"

In the numeric operation, we can use "declare-I Total = $ firstnu * $ secnu" or the above method! Basically, we recommend that you use this method for calculation:

Var = $ (Operation content ))

Not only easy to remember, but also more convenient, because the two parentheses can add blank bytes!

[[email protected] scripts]# echo $(( 13 % 3 ))1

 

Differences in script running modes (source, SH script,./script)

Different script running methods may lead to different results! Especially in bash environments! In addition to the method described in the preceding section, you can run the script by using the source or decimal point!

  • Run the script directly.

Script will use a new bash environment to run commands in the script! That is to say, when this running mode is used, the script is actually run in the bash of the subprogram! "When the subroutine is complete, the variables or actions in the subroutine will end and will not be passed back to the parent program 』!

 

[[Email protected] Scripts] # echo $ firstname $ lastname <= OK. The two variables do not exist! [[Email protected] Scripts] # sh sh02.shplease input your first name: vbird <= This name is the please input your last name: Tsai your full name is: vbird Tsai <= see it! In script running, these two variables take effect [[email protected] Scripts] # echo $ firstname $ lastname <= actually, these two variables do not exist in the bash of the parent program!

When you use a direct running method for processing, the system will give a new bash for us to run the commands in sh02.sh, so your firstname, lastname and other variables are actually running in the bash subroutine. After sh02.sh is run, all data in the bash subprogram is removed. Therefore, when ECHO $ firstname under the parent program is in the exercise above, nothing is visible!


Figure 2.2.1. Run sh02.sh In the subroutine
  • Use source to run the Script: run in the parent program

 

[[Email protected] Scripts] # source sh02.shplease input your first name: vbirdplease input your last name: tsaiyour full name is: vbird Tsai [[email protected] Scripts] # echo $ firstname $ lastnamevbird Tsai <=! Data is generated!

You don't need to log out of the system, but want to write some data ~ /. When bashrc configuration is effective, you need to use "Source ~ /. Bashrc "instead of bash ~ /. Bashrc 』!


Figure 2.2.2 and sh02.sh run in parent program

 

Chapter 2. Simple shell script exercises for shell scripts

Related Article

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.