Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the shell syntax?

Shulou Source: shulou.com Published: 2022-06-02 02:08:48 09月20日 Update

Today, I would like to talk to you about what shell grammar is, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something from this article.

Variable rz= "hufei" # defines static variable date= `date` # assignment, dynamic variable echo $rz # hufeiecho'$rz' #'$rz' cannot use echo "$rz" # hufeiecho "'$rz'" # 'hufei'echo' "$rz" #'$rz' cannot use echo `$ rz` # report error echo $dateecho ${rz} addword # variable concatenation summary: string with double quotes Variable references do not pass the parameter #! / bin/bash # in single quotation marks. This line is not necessarily the file name of echo "execution: $0" in production. Echo "first parameter is: $1"; echo "second parameter is: $2"; echo "third parameter is: $3"; echo "number: $#" # parameter number echo "pass parameter as one character: $*" # spell all parameters into a string echo "PID: $" # shell process pid to prevent error kill./test.sh 1 2 3 debugging

Add-x or #! / bin/bash-x to execute

Array arr= (hello world jike make you) echo ${arr} # defaults to the first echo ${arr [*]} #, separated by spaces echo ${arr [4]} # fourth echo ${# arr [*]} # Array number

Mode 1

If condition1then command1elif condition2 then command2else commandNfi

Mode 2 (command form)

If [$(ps-ef | grep-c "ssh")-gt 1]; then echo "true"; fiif case a = "abc" b = "abc" clockwise stringif [$a = = $b] then echo "an equal b" else echo "a no equal b" fi# numif [$c = = $b]; then echo "c equal b"; else echo "c not equal b"; fi# determines whether dir= "/ data/" file= "/ data/file" if [!-d $dir] exists in the file Then echo "${dir} does not exist"; else echo "${dir} already exists"; fiif [!-f $file]; then echo "${file} does not exist"; else echo "${file} already exists" The fi parameter states that:-d determines whether the object exists, and determines whether the object exists for the directory-f, and determines whether the object exists for the regular file-s, and the length is not 0-nt to determine whether file1 is newer than file2 ["/ data/file1"-nt "/ data/file2"] for loop.

Mode 1

For var in item1 item2... ItemNdo command1 command2... CommandNdonefor strin 'This is a string'do echo $strdone

Mode 2

For var in item1 item2... ItemN; do command1; command2... The done;for loop gets the split string, taking, for example, s = "hello,world,hadoop,jike,ou" # method 1OLDridIFS = "$IFS" IFS= "," arr= ($s) IFS= "$OLD_IFS" for x in ${arr [*]} do echo $xdone# method 2arr2 = (${arr2 [*]}) for x in ${arr2 [*]} do echo $xdonewhile cycle while conditiondo commandcards for example, while ($int)

Tags: Parameters files numbers input functions variables characters tutorials rookie tutorials rookies strings methods selections two number content objects examples loops syntax Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Technology Shulou Information Xiaomi Docker