Linux shell Scripting--building basic scripts (i)

Source: Internet
Author: User
Tags time and date

Create a shell script

When you create a shell script file, you must specify the shell to use in the first line of the file, in the format:

#!/bin/bash

Create and execute SH script

Code 1-1

[Email protected]:/data# cat demo1 #/bin/bashdatewho[email protected]:/data# ls-ltotal 4-rw-r--r--1 root root Dec
   
    1 13:59 demo1[email protected]:/data# chmod u+x demo1 [email protected]:/data# ls-ltotal 4-rwxr--r--1 root root Dec< C2/>1 13:59 demo1[email protected]:/data#/demo1 Thu Dec  1 14:03:57 CST 2016root     pts/0        2016-12-01 13:34 (110 .86.16.210)
   

In code 1-1, chmod u+x demo1 to modify the Demo1 file permissions to make it executable

If you want to display the output of a text string and a command on the same line, you can use the-n parameter of the Echo statement, date must be wrapped, otherwise it will not be printed or printed with the day Code 1-2
[Email protected]:/data# cat Demo2 #!/bin/bashecho-n "The Time and date is:" Datewho[email protected]:/data#./demo2 the Time and date Are:thu Dec  1 20:06:19 CST 2016root     pts/0        2016-12-01 19:59 (122.90.139.156)

Use environment variables, and escape US $ $ sign if you want to print US $ $ required to be escaped, otherwise it is possible to print to an empty or other defined environment variable code 1-3
[Email protected]:/data# cat Demo4 #!/bin/bashecho "User info for userid: $USER" echo "UID: $UID" echo "HOME: $HOME" echo "the C OST of item is $ "echo" The cost of item is \$15 "[email protected]:/data#./demo4 User Info for Userid:rootuid:0home:/roo TThe cost of item was 5The cost of item is $

  

User VariablesIn addition to environment variables, shell scripts allow you to define and use your own variables in scripts, define variables to allow temporary storage of data and use code 1-4 throughout the script
[Email protected]:/data# cat Demo5 #!/bin/bashdays=19guest= "Tom" echo "$guest checked in $days days ago" days=25guest= "Amy "echo" $guest checked in $days days ago "customer= $guestecho" the customer was $customer "[email protected]:/data#./demo5 to M checked in ~ agoamy checked in/agothe customer is Amy

  

Anti-QuoteAnti-quote in shell script allows the output of the shell command to be assigned to the variable code 1-5
[Email protected]:/data# cat demo6 #!/bin/bashtesting= ' date ' echo ' $testing ' today= ' date +%y-%m-%d ' echo ' now $today '  today= ' Date +%h:%m:%s ' echo ' Now is $today ' today= ' date ' +%y-%m-%d%h:%m:%s ' ' echo ' now $today ' tomorrow= ' date-d tomorrow "+%y-%m-%d%h:%m:%s" ' Echo ' Tomorrow is $tomorrow "yesterday= ' date-d yesterday  " +%y-%m-%d%h:%m:%s "' Echo ' Yesterday is $yesterday "[email protected]:/data#./demo6 Thu Dec  1 20:45:05 CST 2016now are 2016-12-01now are 20:45:05now is 2016- 12-01 20:45:05tomorrow is 2016-12-02 20:45:05yesterday was 2016-11-30 20:45:05

  

Output RedirectionSend the output of the command to a file, bash Shell (>) to complete the output redirection, using (>>) to complete the Append output redirection code 1-6
[Email protected]:/data# date > Demo7[email protected]:/data# cat Demo7 Thu Dec  1 20:52:09 CST 2016[email protected ]:/data# who > Demo7 [email protected]:/data# cat Demo7 root     pts/0        2016-12-01 19:59 (122.90.139.156) [Email Pro tected]:/data# date >> Demo7 [email protected]:/data# cat Demo7 root     pts/0        2016-12-01 19:59 ( 122.90.139.156) Thu Dec  1 20:52:32 CST 2016

  

Input REDIRECTRedirect the contents of the file to the command, enter the redirect symbol (<), the inline input redirection symbol is a double less than sign (<<), in addition to this symbol, you must specify a file tag to divide the start and end of the input data The WC command is used to print the file's text lines, the number of words, Default output three value code 1-7 for byte count
[Email protected]:/data# WC <<EOF> java> php> python> EOF 3  3 16

  

PipingSometimes you need to send the output of one command as input code for another command 1-8
[Email protected]:/data# cat demo9 Hello Javahello springhello mybatisphp is the ' best ' language in the ' The BES ' T language in the World[email protected]:/data# cat Demo9 | grep Javahello Javajava is the best language

  

The expr command allows mathematical expressions to be processed on the command (note: Numbers and operators must be separated by spaces), some symbols with special meanings (such as asterisks), plus backslash escape code 1-9
[Email protected]:/data# expr 1 + 56[email protected]:/data# expr 1-5-4[email protected]:/data# Expr 1 * 5expr:syntax E Rror[email protected]:/data# expr 1 \* 55[email protected]:/data# expr 1/50

  

Assigns the result of an arithmetic expression to a variable, which needs to be enclosed in quotation marks. Code 1-10
[Email protected]:/data# cat demo1 #!/bin/bashval1=20val2=10val3= ' expr $val 1/$val 2 ' echo "The result is $val 3" [Email prot ected]:/data#./demo1 the result is 2

  

Use square brackets to calculate

Code 1-11

[Email protected]:/data# val1=$[1+5][email protected]:/data# echo $val 16[email protected]:/data# val2=$[$val 1+10][ Email protected]:/data# echo $val 216[email protected]:/data# cat Demo2 #!/bin/bashval1=5val2=8val3=9val4=1val5=10val6 =2val=$[val1+ (VAL2-VAL3) +val4*val5+val6]echo "The result is $val" [e-mail protected]:/data#./demo2 The result is 16

  

BC Computers allow you to enter floating-point expressions, interpret expressions, evaluate, and return results at the command line, which the BC Calculator recognizes:

    • Number (integers and floating-point numbers)
    • Variables (simple variables and arrays)
    • Comments (lines starting with # or C-/**/)
    • An expression
    • Programming statements (If-then)
    • Function

Code 1-12

[Email protected]:/data# BCBC 1.06.95Copyright 1991-1994, 1997, 1998, $, 2004, 2006 free software Foundation, inc.this is free software with absolutely NO WARRANTY. For details type ' warranty '. 9.1+11.220.38.1+9.918.0quit

  

Use Bc-q to remove the hint code 1-13
[Email protected]:/data# bc-q3/50scale=33/5.600val1=10val2=6val1*val260val3=val1+val2print val316quit

  

Using BC code 1-14 in scripts
[Email protected]:/data# cat Demo3 #!/bin/bashval1= ' echo ' SCALE=3;3/6 ' | BC ' echo ' The result is $val 1 "[Email protected]:/data#./demo3, the result is. 500

  

View exit Status CodesLinux provides a $? variable to hold the exit status code of the last execution command

Code 1-15

[Email protected]:/data# datefri Dec  2 04:57:24 CST 2016[email protected]:/data# echo $?0[email protected]:/data# AAA No command ' AAA ' found, did you Mean:command ' aha ' from the package ' aha ' (Universe) command ' AA ' from the package ' astronomical- Almanac ' (Universe) command ' ARA ' from the package ' ARA ' (Universe) command ' JAAA ' from the package ' JAAA ' (universe) Aaa:command Not Found[email protected]:/data# Echo $?127

  

Linux Exit Status Code
Status code Describe
0 Command completed successfully
1 Generic Unknown error
2 Misuse of SHELL commands
126 Command not executable
127 No orders found.
128 Invalid exit parameter
128+x Critical error for Linux signal x
130 Command terminated with CTRL + C
255 Exit status code out of bounds

Exit Exit command can specify exit status code, but not more than 255, otherwise it will be modulo

Code 1-16

[Email protected]:/data# cat Demo4 #!/bin/bashdateecho $?whoexit 5[email protected]:/data#./demo4 Fri Dec  2 05:03:08 CST 20160root     pts/0        2016-12-02 04:39 (122.91.222.126) [Email protected]:/data# echo $?5

  

Linux shell Scripting--building basic scripts (i)

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.