Some common records of Linux Shell (1)

terminal print

echo

''$ variable within single quotes is invalid, ""$ variable within double quotes is valid, `` backticks are executable commands, and the command results can be assigned to variables

#!/bin/bash
no1 = 2;
no2=3;
let result=no1+no2
echo $result

echo -e parse escape characters, such as \n newline

file redirection

Redirection saves the input text to a file in intercept mode :

echo "this is a text line one" > test.txt

Before writing to the file, the file contents are first emptied.

Redirection saves the input text to a file in append mode :

echo "this is a text line one" >> test.txt

After writing to the file, it is appended to the end of the file.

Redirection of standard error output:

method one:
[root@localhost text]# cat linuxde.net 2> out.txt   //There is no error message, it runs normally.
Method Two:
[root@localhost text]# cat linuxde.net &> out.txt
[root@localhost text]# cat linuxde.net 2> /dev/null

/dev/null is a special device file. Any data received by this file will be lost, usually called bit bucket or black hole.

if conditional statement

if condition test operation
   then command sequence
 fi
#!/bin/bash
#When the space usage of the /boot partition exceeds 80%, output an alarm message.

use=`df -hT | grep "/boot" | awk '{print $6}' | cut -d "%" -f1`
if [ $use -gt 80 ];
  then
    echo "Warning!!/boot disk is full"
be
#!/bin/bash
#Determine whether iptables is running, if it is already running prompt information, if it is not enabled. 
service iptables status &> /dev/null
if [ $? -eq 0 ];
  then
    echo "iptables service is running"
  else
    service iptables restart
be

Conditional tests are testable files, test strings, test integers, etc.

test file

format 
 [operator file or directory]

operator

-d : test if it is a directory, true if it is (Directory) -e : test if a directory or file exists, if it exists (Exist) -f : test if it is a file, true if it is ( file )

if [ -d $a ] #If the path $a is a directory

if [ -e /home/aa.txt ] #aa.txt exists

Integer value comparison

format 
[integer1 operatorinteger2]

operator

-eq : equal (equal) -ne : not equal (not equal) -gt : greater than (Greater than) -lt : less than (lesser than) -le : less than or equal (lesser or equal) -ge : greater than or equal (Greater or equal)

for loop statement

for variable name in value list
 do
 command sequence
done

The most basic for loop

#!/bin/bash
for x in one two three four
do
    echo number $x
done

Do a for loop over files in a directory

#!/bin/bash
for x in /var/log/*
do
   #echo "$x is a file living in /var/log"
   echo $(basename $x) is a file living in /var/log
done

while loop statement

while command expression
 do
  list of commands
done
#!/bin/bash
#Add 20 system accounts in batches and the usernames are user1~20
i=1
while [ $i -le 20 ]
do
 useradd user$1
 echo "123456" | passwd --stdin user$i &> /dev/null
 i=`expr $i + 1`
done

break: jump out of the loop body

continue: End the remaining loop statement and start the loop again.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325312621&siteId=291194637