Linux Shell Scripting Basics Learning _linux Shell

Source: Internet
Author: User
Tags loop case file permissions

Shell script:

1, #! Shell that specifies script execution
2, # Annotation
3. command or grammatical structure
echo "Input"
Echo Output blank line

Perform:

1. Shell Script
SH Example
2, to the shell script execution permissions
chmod U+x Example

Case: Example sysinfo.sh

3ABC x cannot begin with a number
Abc3
ABC3

environment variable-UPPERCASE

Command execution result assignment variable using command substitution '
Path= $PATH:/root/shell.example

Ls-l/etc/inittab/etc/fstab/etc/services
$ $ $ $

$0-$9
$*/etc/inittab/etc/fstab/etc/services
$# 3
$$ PID
$? Command return value 0 non 0

Case: Special.var

Read case: Read

The expr operator takes a space before and after, multiplication * To add an escape character
Case: Expr

Test
1. Whether the string test is equal or empty
2, integer test equal, greater than, less than
3, file test file type, file permissions, file existence, file size

Single Branch cycle: If/then
if test condition
Then
Perform an action
Fi

Apache= '/usr/bin/pgrep httpd '

If ["$APACHE" = ""]
Then
/ETC/RC.D/INIT.D/HTTPD start
Fi

Sh-x script Displays execution + or + + comments at execution time
*/2 12-14 * 3-6,9-12 1-5/root/shell-example/apache.test


Double loop: If/else

if test statement
Then
True: Operation
Else
Fake: Operation
Fi

Case: Test.apache autobak.sh

TAR-CF New file name directory

Multi-branch Cycle: If/elif
Case: If_else judgment file type
Conditional connection-o logic or or-a logic and

Exit Exits statement
If ["$#"!= 2]
Then
echo "must two parm!"
Fi
echo "This is a test"
Echo $ $

SH Exit A
Condition is true exit Exit 0 condition is false exit exit 1 non 0
Add Exit statement SH exit a

For Loop
Case: For automatically kicking the user out of the system killuser.sh

awk-f field Separator ' command ' The default delimiter is a space
Ps-le | grep httpd | awk ' {print $} '
Awk-f: ' $3==0 {print $} '/etc/passwd
Number of users of statistical systems
Awk-f: ' $3>=500 {print $} '/etc/passwd | Grep-v Nfsnobody | Wc-l
Awk-f: ' Length ($) ==0 {print $} '/etc/shadow

Case cases: CASE/ETC/RC.D/INIT.D/HTTPD

While condition is true loop case: while useradd.sh
Until condition is false always loop

echo Password | passwd--stdin User Name

While Count Loop

Windows Write script will have an unrecognized control character to handle

Dos2unix Script

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.