Shell script preliminary Learning

Source: Internet
Author: User
Tags integer numbers

It is very important for Linux system administrators and O & M personnel to learn Shell scripts. 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. Good habits of writing shell scripts 1. script functions; 2. script version information; 3. Author and contact information; 4. script version declaration method; 5. script History (History) 6. Execute special commands in the script using the "absolute path" method; 7. Environment Variable pre-declaration and settings required for script execution. Sh01.sh [plain] <span style = "font-size: 14px;"> #! /Bin/bash # program: # This program show "Hello World! "In your screen # History: #2013/04/21 :31 ccf First release PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin: /usr/local/sbin :~ /Bin export PATH echo-e "Hello World! \ A \ n "exit 0 </span> sh02.sh [plain] <span style =" font-size: 14px; "> #! /Bin/bash # Program # User inputs his first name and last name. program shows his full name. # History #2013/04/21, by ccf, First release PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin: /usr/local/sbin :~ /Bin export PATH read-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 "# result output by screen </span> sh03.sh [plain] <span style =" font-size: 14px; "> #! /Bin/bash # Program creates three files, which named by user's input # and date command # History: #2013/04/21 Sunday ccf First release PATH =/bin:/sbin: /usr/bin:/usr/sbin:/usr/local/bin:/usr/loacl/sbin :~ /Bin export 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 date3 = $ (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 a file name: touch "$ file1" touch "$ file2" touch "$ file3" </span> sh04.sh [plain] <span style = "font-size: 14px;"> #! /Bin/bash # Program: # User inputs 2 integer numbers; program will cross these two numbers. # History: #2013/04/21 ccf19881030 First release PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin: ~ /Bin export PATH echo-e "You shoshould input 2 numbers, I will cross them! \ N "read-p" first number: "firstnum read-p" second number: "secondnum total = $ ($ firstnum * $ secondnum )) echo-e "\ nThe result of $ firstnum * $ secondnum is ==>$ total" </span> sh05.sh [plain] <span style = "font-size: 14px; "> #! /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 Release PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin: /usr/local/sbin :~ /Bin export 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" </span> sh06.sh [plain] #! /Bin/bash # Program: # This program shows the user's choice # History: #2013/05/14 ccf First release PATH =/bin:/sbin:/usr/bin: /usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin export PATH read-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 0 echo" I don't know what your choice is "& exit 0 can run shell scripts through sh sh01.sh or chmod a + x sh01.sh;./sh01.sh.

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.