Linux Shell Programming Study Notes

1: Shell Script condition section if statement to be separated by semicolons

2: test conditions to be noted in the space portion. In both brackets have spaces

3: echo "Hi, $ { a} s" variable is not in single quotes variable substitution operations.

4: [-f "$ file" ] $ file determines whether a file

5: [$ a -lt 3] $ a is determined whether the value of 3, and the same -gt -le represent greater than or equal to less than

6: [ -x "$ file"] and determines whether there is a $ file executable permissions, the same test file -r readability

7: [-n "$ a" ] determines whether a value of the variable $ a, the test string with the empty -z

8: [ "$ a" = "$ b"] Analyzing and $ a $ b values are equal to

9: [cond1 -a cond2] cond1 and determines whether the setting up of cond2, -o represents a cond1 established and cond2


Description: $ # represents the number including $ 0, including the command-line parameters. In the Shell, the name of the script itself is $ 0, followed by the remaining $ 0, $ 1, $ 2 ..., $ {10}, {11} $, and the like. $ * Represents the entire parameter list, not including $ 0, that does not include the parameter list of file names.

Note: Be sure to remember that not a single quotation marks filelist = behind, but the tab key on top of the key, or a key that is left 1

filelist=`ls /home/work`
for file in $filelist
do 
echo $file
done
 

Example: The following shell traverse directory of all files  displayfile.sh

#!/bin/sh
cd $1
echo "The Catelog:$1"
files=`ls -a`
m=0
n=0
for f in $files
do
 if [ -d "$f" ]; then
  m=`expr $m + 1`
 else
  n=`expr $n + 1`
  echo "$f"
 fi
done
echo -e "The Catelog Number is $m"
echo -e "The File Number is $n"
运行:./displayfile.sh   /home/work

Examples: Linux shell script to determine whether the current user is root

whoami (to display the current user name)

IF [ `whoami` =" root "]; the then
 "! root user "echo
the else
 echo" non-root user! "
Fi

id -u (the current user's uid)

if [ `id -u` -eq 0 ];then
 echo "root用户!"
else
 echo "非root用户!"
fi

转载于:https://www.cnblogs.com/hubcarl/p/3334008.html

Guess you like

Origin blog.csdn.net/weixin_34090643/article/details/93817343