June 04, 2018

Shell script grammar

[List]

❑ list="A1 B1 C1 D1"
❑ AAA=$(echo {101..105} → AAA="101 102




[For]

❑ for ele1 in $list
do
  echo "This is" $ele1
done
❑ for num1 in {100..105}
do
done




[While]

❑ while [ : ]
do
done




[Case]

❑ case $var1 in
1) echo 1 ;;
2) echo 2 ;;
3) echo 3 ;;
4) echo 4 ;;
esac




[If]

❑ if [ $var1 == "n" ]
then
echo "n"
else
echo "not n"
fi
❑ if [ -e ./test.txt ] : True if the "./test.txt" is exists, false if not.
❑ if [ $? -eq 0 ] : True if exit status is 0.
❑ if [ $PERCENT -ge "30" ] : $PERCENT >= 30

Arithmetic operatorDescription
val1 –eq val2True if the val1 and the val2 is equal.
val1 –ne val2True if the val1 and the val2 is not equal.
val1 –gt val2>. True if the val1 is greater than the val2.
val1 –lt val2<. True if the val1 is less than the val2.
val1 –ge val2>=. True if the val1 is equal or greater than the val2.
val1 – le val2<=. True if the val1 is equal or less than the val2.

String operatorDescription
-z val1True if the val1's length is 0.
-n val1True if the val1's length is not 0.
va1 = val2
val1 == val2
True if the val1 is equal to val2.
val1 != val2True if the val1 is not equal to val2.
val1True if the val1 is not null string.

Logical operatorDescription
val1 -a val2AND operator.
val1 -o val2OR operator.

File operatorDescription
-b file1True if the file1 is a block device file.
-c file1True if the file1 is a character device file.
-d file1True if the file1 is a directory file.
-S file1True if the file1 is a socket file.
-t file1True if the file1 is a opened terminal file.
-e file1True if the file1 is exist.
-s file1Ture if the file1's size is greater than 0.
-f file1True if the file1 is a regular file.
-O file1True if the file1's owned user is current user.
-G file1True if the file1's owned group is current group.
-u file1True if the file1 has the SetUID permission.
-g file1True if the file1 has the SetGID permission.
-k file1True if the file1 has the Sticky bit permission.
-h file1
-L file1
True if the file1 is a symbolic link file.
-p file1True if the file1 is a named pipe file.
-r file1True if the file1 has the read permission.
-w file1True if the file1 has the write permission.
-x file1True if the file1 has the execute permission.
file1 -nt file2True if the file1 is newer than the file2.
file1 -ot file2True if the file1 is older than the file2.
file1 -ef file2True if the file1 is equal(Hard link) to the file2.




[Misc]

❑ echo $(date '+%y')"year "$(date '+%m')"month "$(date '+%d')"day "$(date '+%H')":"$(date '+%M')":"$(date '+%S') → 18year 06month 04day 01:02:10
❑ echo $(date '+%y%m%d%H%M%S') → 180604010210
❑ ${fileName%*.exe} : Remove ".exe" at the end of the fileName.
❑ $1 : The first argument of the program.
❑ $2 : The second argument of the program.
❑ read var1
❑ read -p "Enter Your Username?[user] → The user's input data will be stored in the variable var1.
* p : Option to receive user's input in one line including guide.
❑ echo "1+2" | bc → 3
❑ echo "scale=3;10/3" | bc → 3.333
* bc(Basic Calculator) : Perform the arithmetic operations.