Follow Me---quickstart shell scripting (ii)

Source: Internet
Author: User
Tags arithmetic bit set readable

Last time I wrote the shell, I found that VI and VIM are different: Vim is the upgrade version of VI, it is not only compatible with all VI directives, but also some new features in it. Vim is much more useful than VI.

This time the last content, the basic knowledge, continue to study, the author also in self-study, error is unavoidable, if someone found the problem please point out thank you!

Lee produced, reproduced please specify the source http://blog.csdn.net/hnulwt/article/details/43155797

Boolean operator

Get a general familiarity with the boolean-related three operators first

! Non-operational

-O or operation (or)

-A with operations (and)

Then write the program to familiarize yourself with:

  1 #!/bin/sh  2  3 a=4  4 b=6  5  6 if [$a! = $b]  7 then  8         echo "$a! = $b A is isn't equal to B"  9 Else Ten         echo "a = B" one fi ($a-gt 3-a $b-lt) and then         echo "and && true" LSE         Echo "A < 3 or B > ten" fi
Operation Result:

[Email protected]/desktop
$ sh test.sh
4! = 6 A is no equal to B
and && True

Boolean operators are relatively simple, and we usually use the programming language is not very different, just pay attention to his or arithmetic and operation symbols can be.

Shell scripts have two special types of operators: 1, string-related operator 2 file test operators

String and string operators

Referring to the string, tell the shell string, he is probably the most commonly used in the shell data type (except that he has no other type), first to see how the string is represented

Strings can be in single or double quotes, or without quotes

Single quotation marks

Two points to note:

Any character in a single quotation mark is output as is, and the variable in the single-quote string is not valid;

Single quotation marks cannot appear in single quote string (not after using escape character for single quotes)

Double quotes

Double quotes basically no restrictions, commonly used double quotes, double quotes inside can use the escape character, you can bring into the variable

Cases:

  1 animal= "Dog"  2 say= "Hi, \" $animal \ ""  3  4 echo $say
Operation Result:

$ sh teststr.sh
Hi, "Dog"

You can see that the two colons are escaped, and the string represented by animal is replaced with a new sentence.

Here's a couple of ways to get a string.

1, the method of getting the string length

  1 animal= "Dog"  2  3 echo ${#animal}
Output: 3 (Cannot use echo $ #animal here, you can test, the output is not right, so that $ #会代表 passed to the script or function parameters, so output: 0animal)

2, finding the string

  1 animal= "dog, pig, cat, lion and so on"  2  3 echo ' expr index ' $animal ' cat '

Output: 11 (note the third line here, expr and cat have a special symbol after it, the symbol is not a single quotation mark, the one below the ESC key)


OK, then let's see what the string operators are

= and! = To determine whether two strings are equal (remember to determine whether the numbers are equal in arithmetic Fu Yi (-eq and-ne))

-Z,-N, or directly into the string to determine whether the string length is 0 (-Z when the string length is 0 returns True)

See the test procedure below:

  1 #! /bin/sh  2  3 a= "ABASDF"  4 b= "Sdfa"  5  6 if [$a = $b]  7 then  8         echo "1"  9 else         echo "2" one fi-if [-Z $a] and then         echo "3"-Else +         echo "4" (FI) if [$a]         echo "5" All Else         echo "6" fi

Look at the results of the operation:

$ sh test.sh
2
4
5

File Test Operators

First we create a file on the desktop and execute the command touch file.test

At this point we have created the file, but we do not know what his permissions are, run the command: ls-l | grep file.test View permissions, run as shown in the results:


Here we make a brief introduction to Linux permissions:

The permissions at the beginning are-(regular files), and possibly the beginning,

"D" directory (very common, you can find a directory, in the directory of the previous level of the directory ls-l to see)
"L" symbolic link (some links created through LN)
"C" character specialized device file
"B" block specialized equipment files
"P" Advanced first Out
"S" sockets

And then we started looking at the first--the following field, three for a group look, the first ternary character group (rw-) represents the permissions of the file owner, the second (r--) represents the permissions of the group of the file, and the third (r--) represents the permissions of all other users

What does R and W mean, respectively? Represents readable read and writable write respectively. There is a current file does not have this permission, that is, x executable (execute), then we have what permissions now, generally we have the first ternary character group represents the permission, that is: readable writable

Well, then, what do I do to him? executable permissions chmod +x file.test, which gives three file owners permission to execute, we can also perform chmod u+x file.test just give us permission.

For a detailed description of the chmod directive, you can perform chmod--help view

Now the permissions have been assigned to the file. The preparation is ready and we are ready to test this new file.

Write the following shell for proficiency and testing:

  1 #! /bin/sh  2  3 file= "/desktop/file.test"  4  5 If [-R $file]  6 then  7         echo "1"  8 fi  9 If [-W $file] One and then         echo "3" Else         echo "4" [f $file] and then         echo "fil  E is an ordinary file "all else         echo" special file "the fi max if [-D $file] and then         echo" Directory "27 else         echo "Not Direcotry" fi


Operation Result:

$ sh test.sh
1
3
File is an ordinary file
Not Direcotry

may also be unfamiliar with some of the above operators, with a detailed list of operators below.

Operator Description
-B File Detects if the file is a block device file, and returns True if it is.
-C file Detects if the file is a character device file, and returns True if it is
-D File Detects if the file is a directory, and returns True if it is.
-F File Detects if the file is a normal file (neither a directory nor a device file), and returns True if it is.
-G file Detects if the file has a SGID bit set, and returns True if it is.
-K File Detects if the file has a sticky bit set (Sticky bit), and returns True if it is.
-P File Detects if the file is a named pipe, and returns True if it is.
-U file Detects if the file has a SUID bit set, and returns True if it is.
-R File Detects if the file is readable and returns true if it is.
-W File Detects if the file is writable and returns true if it is.
-X File Detects if the file can be executed and, if so, returns True.
-S file Detects whether the file is empty (the file size is greater than 0) and does not return true for null.
-E File Detects whether the file (including the directory) exists and, if so, returns True.


Follow Me---quickstart shell scripting (ii)

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.