Shell basic applications and examples

Source: Internet
Author: User
Tags arithmetic

Shell basic applications and examples

One

$# the number of arguments to the script

$* displays all parameters passed to the script in a single string. Unlike positional variables, this option parameter can be more than 9

$$ the current process ID number for the script to run

$! Process ID number of the last process running in the background

[Email protected] is the same as $*, but quotes are used, and each parameter is returned in quotation marks

$? Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error.

$ A script name

$1..$9 Nth Parameter


Second, the function basic use:

function functname{

Shell commands

}

Or

Functname () {

Shell commands

}

Third, Bash's arithmetic operation comparison operator \ string comparison operator \ File comparison operator:

Tea tasting: This one is relatively full. Check the general, please click here.

1. Arithmetic Operation comparison operator (bash itself cannot compare floating-point numbers)

-eq equals [$num 1-eq $num 2]

-ne not equal to [100-ne $num 1]

-lt less than [100-lt ' expr $num 1 + $num 2 ']

-le less than or equal to [100-le ' expr $num 1 \* $num 2 ']

-GT greater than [100-gt ' expr $num 1/$num 2 ']

-ge greater than or equal to [100-ge ' expr $num 1 $num 2 ']

2. String comparison operators

-Z String True if string length is zero [-Z ' PS aux | grep MySQL ' "]

-N String if string length is nonzero, true [-n ' $string ']

"Note" $string must be placed in double quotation marks inside "$string", otherwise the result of using-n-z is true!

String1! = string2 true if string1 is different from string2 ["$str 1"! = "Snail"]

string1 = = string2 If string1 is the same as string2, then True ["$str 1" = = "$str 2"]

(above with one = can also be used under strict POSIX compatibility)

String1 string2 if string1 is more than string2 in dictionary order, true

Iv. File comparison Operators

-a filename if filename exists, true [-e $HOME/.BASHRC]

-e filename (ibid.)


-d filename If filename exists and is a directory, true [-d/home/snail]

-f filename If filename exists and is a regular file, true [-F/DEV/TTYS0]


-r filename If filename exists and is readable, true [-r/etc/passwd]

-u filename if filename exists and is Set-user-id, true [-f $HOME/ak47]

-W filename if filename exists and is writable, true [-w/var/log/mail]

-x filename If filename exists and is executable, true [-X./start.sh]

Five, a few common grammar

While basic usage:

while [condition]

Do

Command1

Command2

Command3

Done

#!/bin/bash

sudo find-type f-name ' *.log ' |while read mvname

Do

MV $mvname ${mvname/.log/. LOG}

Done

#!/bin/bash

#查找当前目录下 *.log and assign the value to Mvname to participate in the loop.

sudo find-type f-name ' *.log ' |while read mvname

Do

#移动, and replace the ending. Log with a. Log/*ubuntu.

MV $mvname ${mvname/.log/. LOG}

Done

Example: Above is a case of a batch replace file name bash

Simple PD file type, which can be written according to the above File Class form table:

#!/bin/bash

If [$#-ne 0]; Then

If [-D $]; Then

Echo is a Mulu

Else

echo $ D1 D2

Fi

If [-D + $]; Then

echo $ is a Mulu

Else

echo $ D1 D2

Fi

Fi

#!/bin/bash

#如果个数不为空的话

If [$#-ne 0]; Then

#如果 The first parameter is a table of contents

If [-D $]; Then

Echo is a Mulu

Else

echo $ D1 D2

Fi

If [-D + $]; Then

echo $ is a Mulu

Else

echo $ D1 D2

Fi

Fi

The program is just an example, there are many bugs in the middle, for example, if there are three parameters?

To make into a function to express:

#!/bin/bash

Set-x

ISD1 () {

echo $ D1 D2

}

If [$#-eq 2]; Then

If [-D $]; Then

Echo is a Mulu

Fi

If [-D + $]; Then

echo $ is a Mulu

Fi

Else

Isd1

Fi

Calculate the value change:

[Email protected]:/bash$ cat nubmer.sh

#!/bin/bash

A=1

b=2

Echo $a + $b

echo $ (($a + $b))

[Email protected]:/bash$ sudo sh nubmer.sh

1+2

3


This article is from the "Ten Years Sword-Breeze" blog, please be sure to keep this source http://jingfeng.blog.51cto.com/9152964/1880760

Shell basic applications and examples

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.