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