Getting started with shell scripts

Source: Internet
Author: User
Shell script entry 001 #! /Bin/bash002fruitapple003count5004echo & quot; Wehave $ count $ {fruit} (s) & quot; 005006007 # Use shell variable 008 # shell environment variable 009 echo $ PATH010011012 # Add a new path 013 exportPA shell script entry 001 #! /Bin/bash002fruit = apple003count = 5004 echo "We have $ count $ {fruit} (s) "005 006 007 # Use shell variable 008 # shell environment variable 009 echo $ PATH010 011 012 # Add a new PATH 013 export PATH =" $ PATH: /home/usr/bin "echo $ PATH014 015 016 # obtain the string length 017var = 123456 echo $ {# var} 018 019 020 # obtain the current shell version 021 echo $ SHELL022echo $0023 024 025 # check whether the current user is a superuser 026if [$ UID-ne 0]; then027echo no root user please run as root028else echo 'root user' s029 030 031 # Let no1 ++, let no1 =-let no + = 6 let no-= 6032no1 = 4033no2 = 5034let result = no1 + no2035echo $ result036 037 038 # redirect File> overwrite File 039 echo "this is a sample 1 "> temp.txt 040 # Redirection> append file 041 #0-standard input 042 #1-standard output 043 #2-standard error 044 echo" this is a sample 2"> temp.txt echo ls> dd.txt 2> & 1 0451_error messages and standard information are fully redirected to 046 047 048 in dd.txt # define and use arrays 049arr = (1 2 3 4) 050 echo $ {arr [0]} 051 052 053 # Another way of life 054arr [0] = "test1" 055arr [1] = "Test2" 056 echo $ {arr [0]} 057 index = 5 echo $ {arr [$ index]} 058 059 # join array 060 # declare separately -a arr061 062arr [0] = wangdk; 063arr [apple] = iphone5064 065 can also be used with 066arr = ([0] = wangdk [1] = test) 067 068 # List array indexes 069 echo $ {$ arr [*]} 070 echo $ {! Arrw [@]} 071 072 # command alias 073 alias new_command = 'command "074 # temporarily effective alias 075 alias install = 'yum-y install '076 # permanently effective 077 echo 'Alias cmd = "command" >> ~ /. Bashrc078 079 # shield the terminal from inputting 080 echo-e "Enter password: "081 stty-echo # shield output 082 read password083stty echo # Open output 084echo085echo Password read000086 087 088 # time-related command script 089 date # time 090 date + % s time stamp 091 date -- date "12:00:00" + % s date to timestamp 092 date -- date "@ 1370059200" + % D time stamp to date 093 date -- date "@ 1370059200" "+ % Y/% m/% d % H: % M: % S "094 095 # time required to check a group of commands 096 #! /Bin/bash097start = $ (date + % s) 098commands099end = $ (date + % s) 100 difference = $ (end-start )) 101 102 103 # sleep computing time loop 104 105 # output a count1_echo-n Count107 # store the cursor position 108 109 tput sc110 111 # Initialize the variable 112 count = 0113 while true114do115 # if <401_if [$ count-lt 40]; 117 # Auto-increment 118 then let count ++; 119 sleep 1; 120 121 # Restore the cursor 122 tput rc123tout ed124 # output 125 echo-n $ count; 126 else exit 0; optional fi128done129 130 # shell function definition parameter 131 #/bin/ba Sh132 # filename function133function fname () 134 {135 echo $1, $2; # print parameter 1, parameter 2136 echo "$ @"; # print 137 echo "$ *" at a time in the form of a list; #138 return 0; # return value 139} 140 fname wangdk, sunshuzhen is printed in units similar to $; # Call the function 141 echo $? # Recent function return value 142 143 # pre-read command 144__output = $ (ls | cat-n) 145 echo $ cmd_output146 147 # Another anti-reference 1481__output = 'ls} cat-n' 149echo $__output150 151 # retain spaces and linefeeds 152cat text.txt # output filter by referencing the sub-shell space 153 154out = "$ (cat text.txt) "155 echo $ out # unchanged 156 157 # rand command read command line character 158 159 read-n 2 var # read 2 characters to var variable 160 echo $ var161 162 read-s var # read password 163 echo $ var164 165 read-p "Enter input: "var166echo $ var; 167 168 read- T 2 var # read the variable 169 echo $ var170 171 read-d ":" var # encounter character: variable 172 echo $ var173 174 # IFS character separator and iterator 175 # IFS is the delimiter reserved by the system. by default, it is a blank character, and the TAB and space are 176 data = "name, sex, rollno, location "177 oldIFS = $ IFS178IFS =, 179for item in $ data180do181echo Item: $ item182done183IFS = $ oldIFS184 185 186 # IFS example 187 #! /Bin/bash189 # filename1_data = "root: x: 0: 0: root:/bin/bash" 191 oldIFS = $ IFS192IFS = ": "193 count = 0194for item in $ data195do196 [$ count-eq 0] & user = $ item197 [$ count-eq 6] & shell = $ item198let count ++ 199done200echo $ user201echo $ shell202IFS = $ oldIFS203 204 # do not know if other commands except for item in $ data support the 205 # shell loop iteration 206 207for var in list; 208do209commands; 210done211 212 # Generate the array sequence 213 214for I in {.. z}; 215for I in (1 .. 50}; 216 217 while condition218do219commands; 220done221 222 condition use true as the loop condition to generate a wireless Loop 223 224 225 until loop 226 in bash, you can also use a special loop until which will keep repeating to the given condition 227x = 0228 until [$ x-eq 9]; 229do230let x ++ 231 echo $ x232done233 234 235 # logical judgment structure 236if condition; 239fi240 241if condition; 245else246 condition 249 # Concise version 250 [condition] & action; # If condition If the condition is true, execute actioncondition [condition] | action # if the condition is false, then execute action252 253 # & is a logical operator | logical or operator 254 255 #-gt greater than 256 #-lt less than 257 #-ge Greater than or equal to 258 #-le less than or equal to 259 #-eq = 260 #-ne not equal to 261 262 # file system test 263 [-f $ file_var] # if a specified variable contains a normal file path or file name, returns true 264 [-x $ var] # returns true 265 [-d $ var] if the given variable contains an executable file. # if the given variable contains a directory, returns true 266 [-e $ var] # returns true 267 [-c $ var] if the variable contains a file # if the variable contains a character path of the device file, returns true 268 [-B $ var] # returns true 269 if the variable contains the path of a device file [- W $ var] # returns true 270 if the variable file is writable [-r $ var] # if the variable file is readable, returns true 271 [-L $ var] # if the variable contains a symbolic link, returns true 272 273 # string comparison 274 [[[$ str1 = $ str2] # returns true 275 for equality [[$ str1 = $ str2] # returns true 276 for equality [[ $ str1! = $ Str2] # returns true 277 if not equal [[$ str1> $ str2] # if the alphabetic order of str1 is greater than that of str2, returns true 278 [[[$ str1 <$ str2] # if the alphabetic order of str1 is smaller than that of str2, returns the true 279 [[-z $ str] # if str1 contains a null string, returns the true 280 [[-n $ str] # if str1 contains a non-null string, returns true 281 282 # combination judgment 283if [[-n $ str] & [[-z $ str2]; 284then285commands; 286fi287 288 # The test Command can save the 290if [$ var-eq 0]; then echo "true"; fi291if test $ var-eq 0; then echo 'true'; fi
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.