Shell script preliminary learning-basic learning of laruence Linux Private food

Source: Internet
Author: User
Tags date1 integer numbers

Shell scripts are important for Linux system administrators and O & M personnel. I recently read the shell script. In order to systematically learn the shell script, I read the third edition of "laruence's Linux basic learning for Private food, chapter 1 describes how to learn shell scripts. You can go to the bird's website: Chapter 13th, learning Shell
Scripts takes a look at the examples in this chapter. It is good for beginners to learn shell scripts!

Good habits of writing shell scripts
1. Script functions;
2. script version information;
3. The author and contact information of the script;
4. script version declaration method;
5. Script history (History)
6. Execute special commands in the script in "absolute path" mode;
7. Environment Variable pre-declaration and settings required for script execution.

Sh01.sh

#!/bin/bash# program:# This program show "Hello World!" in your screen# History:# 2013/04/21 15:31 ccf First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHecho -e "Hello World! \a \n"exit 0

Sh02.sh

#! /Bin/bash # Program # user inputs his first name and last name. program shows his full name. # history #2013/04/21, by CCF, first 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 user input echo-e" \ Nyour full name is: $ firstname $ lastname "# The result is output by the screen

Sh03.sh

#! /Bin/bash # Program # program creates three files, which named by user's input # And date command # History: #2013/04/21 Sunday CCF first releasepath =/bin:/sbin: /usr/bin:/usr/sbin:/usr/local/bin:/usr/loacl/sbin :~ /Binexport path #1. ask the user to input 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, use the variable function to analyze whether the file name has been set to filename =$ {fileuser:-"FILENAME"} # Start to determine whether there is a configuration file name #3. start to use the date command to get 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 following three lines are configured in the file name file2 =$ {filename }$ {date2} file3 =3 {filename }$ {date3} #4. create File Name touch "$ file1" Touch "$ file2" Touch "$ file3"


Sh04.sh

#!/bin/bash# Program:# User inputs 2 integer numbers; program will cross these two numbers.# History:# 2013/04/21 ccf19881030  First 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: "  firstnumread -p "second number: " secondnumtotal=$(($firstnum * $secondnum))echo -e "\nThe result of $firstnum * $secondnum is ==> $total"


Sh05.sh

#! /Bin/bash # program: # user input a filename, program will check the follwing: #1.) exist? 2.) file/directory? 3 .) file permissions # History: #2013/05/14 CCF first releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin: /usr/local/sbin :~ /Binexport path #1. Ask the user to input the file name and determine whether the user has an input string echo-e "Please input a filename, I will check the filename's type and permission. \ n "# purely display information read-P" input a filename: "FILENAME # Prompt user input test-Z $ filename & Echo" you must input a filename. "& Exit 0 #2. Determine whether the file exists. If not, display the information and end the Script test! -E $ filename & Echo "the filename '$ filename 'do not exist" & Exit 0 #3. Start to judge the file type and attribute test-F $ filename & filetype = "Regular file" test-d $ filename & filetype = "directory" test-r $ filename & perm = "readable" test-W $ filename & perm = "$ perm writeable "test-x $ filename & perm =" $ perm executable "#4. Start to output information! Echo "the filename: $ filename is a $ filetype" Echo "and the permission are: $ perm"

Sh06.sh

#!/bin/bash# Program:#     This program shows the user's choice# History:# 2013/05/14 ccf First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHread -p "Please input (Y/N) : " yn[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0[ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!" && exit 0echo "I don't know what your choice is" && exit 0

You can run shell scripts through SH sh01.sh or chmod A + x sh01.sh;./sh01.sh.

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.