Shell Programming (i)

Source: Internet
Author: User
Tags syslog

Variable:

1. Variable declaration eg:

#:name= ' EKO '
#!/bin/bashMyurl="http://www.w3cschool.cc"readonly myurl      / /declare read-only variables

2. Variables using eg:

#:echo $name #: Echo ${name}  

3. Delete variables eg:

#: Uset Name

4. Variable type:

    • Local variables Eg:
#:local name= ' Eko '
    • environment variables Eg:
#:export name= ' EKo '

5. View variables

#:printenv     //View all current environment variables #:set         //View all current variables

Format:

The first line of the shell script must be #! /bin/bash

Conditional judgment: (integer comparison, character comparison, file judgment)

An expression:

[Expression] [[expression]] test expression

Branch statements:

Branch statement if  judgment condition then    statement1    statement2 ...    else    ... fi multi-branch if  judging condition then    statement1    statement2    elif    statement1    statement2    ... else    statement1    statement2    ...      Fi

  

Integer comparison:

-EQ: Determine if the value equals eg: # [$a-eq $b]  returns equal to 1  unequal return 0-ne: Determines whether the value equals eg: # [$a-ne $b]  returns 0  unequal return 1-GT: greater than judgment-LT: small To judge-ge: greater than or equal to-le: less than or equal to

Example: Determine file size if larger than 100 lines output large file otherwise output small file

#!/bin/bashlines= ' wc/etc/passwd ' echo $LINESNEWLINES = ' echo $LINES | cut-d '-f1 ' [$NEWLINES-gt] && echo ' This is big file ' | | Echo ' This is small file '

Or

#!/bin/bashfile_path= '/home/eko/passwd ' if [' Wc-l $FILE _path | cut-d '-f1 '-gt ']then echo ' This is big FILE ' else E Cho ' This is small file ' fi

File test

-e filename  if filename exists, true  [-e/var/log/syslog]-d filename  if filename is directory, true  [-d/tmp/mydir]-f fi Lename  True if filename is a regular file  [-f/usr/bin/grep]-l filename  If filename is a symbolic link, true  [-l/usr/bin/grep]-r FileName  If filename is readable, true  [-r/var/log/syslog]-w filename  If filename is writable, true  [-w/var/mytmp.txt]- x filename  If filename is executable, true  [-l/usr/bin/grep]filename1-nt filename2  If filename1 is newer than filename2, true c15/>[/tmp/install/etc/services-nt/etc/services]filename1-ot filename2  If filename1 is older than filename2, then true  [/ Boot/bzimage-ot Arch/i386/boot/bzimage]

 

#shell判断文件夹是否存在 #如果文件夹不存在, create a folder if [!-D "/top"]; Then mkdir-p/TOPFI #shell判断文件 whether the directory exists or has permissions folder= "/top" file= "/top/test.txt" #-x parameter to determine if the $folder exists and has enforceable permissions if [!-X "$folder"]; Then mkdir the "$folder" FI #-D parameter to determine if the $folder exists if [!-D "$folder"]; Then mkdir the "$folder" FI #-F parameter to determine if $file exists if [!-F "$file"]; Then touch "$file" fi#-N to determine if a "variable" has a value if [!-n "$file"]; Then echo "$file variable is empty! "Exit 0fi # Determines if the string contents of two variables are the same if [" $file 1 "=" $file 2 "]; Then echo ' $file 1 equal $file 2 "Else echo" $file 1 Not equal $file 2 "fi

Exit script

Exit [Status Code]: Exit Script

If no status code is specified, then the status code of the last command executed is the exit status code

Positional Parameters/Special parameters

#!/bin/bashecho "First argument is $" echo "second argument is"

$: First parameter

$ A: The second parameter

...

$#: Number of parameters

$*: Parameter list

[email protected]: parameter list

[Email protected]:~$/test.sh First Secondfirst argument is Firstsecond argument

  

Shell Programming (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.