Linux shell Script Learning Note one

Source: Internet
Author: User
Tags syslog

First, file comparison operators
-e filename true if filename exists [-e/var/log/syslog]
-D filename True if filename is a directory [-d/tmp/mydir]
-F filename True if filename is a regular file [-f/usr/bin/grep]
-L filename True if filename is a symbolic link [-l/usr/bin/grep]
-R filename True if filename is readable [-r/var/log/syslog]
-W filename if filename is writable, true [-w/var/mytmp.txt]
-X filename is true if filename is executable [-l/usr/bin/grep]
Filename1-nt filename2 If filename1 is newer than filename2, then true [/tmp/install/etc/services-nt/etc/services]
Filename1-ot filename2 If filename1 is older than filename2, then true [/boot/bzimage-ot Arch/i386/boot/bzimage]
String comparison operators (note the use of quotation marks, which is a good way to prevent whitespace from disturbing the code)
-Z String True if string length is zero [-Z ' $myvar ']
-N String if string length is nonzero, true [-n ' $myvar ']
string1= string2 If string1 is the same as string2, then true ["$myvar" = "One of the three"]
string1!= string2 If string1 differs from string2, true ["$myvar"! = "one, three"]
Arithmetic comparison operators
Num1-eq num2 equals [3-eq $mynum]
Num1-ne num2 Not equal to [3-ne $mynum]
Num1-lt num2 less than [3-lt $mynum]
Num1-le num2 less than or equal to [3-le $mynum]
NUM1-GT num2 greater than [3-GT $mynum]
Num1-ge num2 greater than or equal to [3-ge $mynum]

second, the use of arrays
1.How do I declare an array variable?
array[0]= "Zero" array[1]= "one" array[2]= "one"
OR
Declare-a Array
OR
Array= (Zero one)
OR
Array= "Zero One"
OR
array= ([0]= "Zero" [1]= "one" [2]= ")
2. How do I traverse an array? For ((i=0;i<${#array [@]};i++)] do echo ${array[i]}; Done OR for I in ${array[@]};d o echo $i;d one; 3.

Gets the length of the array

echo ${#array [@]} OR echo ${#array [*]}

4. Deleting an array element

Unset Array[0]

5. Deleting an array

unset array

6. Tiles like Python

Echo ${array[@]:0:2}

Echo ${array[@]:2}

Third, Linux shell check process pid

#
# Check the PID of such program
#
Checkpid () {
If [-z] ' PS x | grep $ | Grep-v grep | Grep-v | awk ' {print '} ' "]; Then
echo "the program cant run well."
Fi
}

Brief description:

PS Reporting Program status.

PS x shows all the programs, not the terminal to distinguish.

The first parameter of the function, such as: Checkpid test, then: $ = test,$0 = Checkpid Test.

Grep finds the strings in the file that match the criteria.

PS x | grep displays the status of the program that contains the keyword ' $ '.

PS x | grep $ | Grep-v grep | Grep-v to remove the influence of its own command

awk ' {print '} ' extracts the packet and formats the display of the command execution

Such as:

PS x | grep Test | Grep-v grep | Grep-v "Checkpid test"

After execution appears as:

12040 pts/1 Ss 0:00 test

So

PS x | grep Test | Grep-v grep | Grep-v "Checkpid Test" | awk ' {print '} '

is displayed as:

12040

That is the PID of the process.

Linux shell Script Learning Note one

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.