Learn shell scripts

Source: Internet
Author: User

Assigning values to variables

Defining an integer class using Declare-i

[[email protected] scripts]# Aa=5+6[[email protected] scripts]# echo $aa 5+6[[email protected] scripts]# declare-i Aa[[ema Il protected] scripts]# aa=5+6[[email protected] scripts]# echo $AA 11

With let, you don't need to define an integral type

[[email protected] scripts]# let Bb=1+8[[email protected] scripts]# echo $bb 9[[email protected] scripts]# cc=$ ((5+8)) [[Em AIL protected] scripts]# echo $CC 13

echo $? return value

Test compare two worth of size

-eq-lt-le-gt-ge

Aa=3 bb=4

Test $aa-eq $BB

echo $?

Test $aa-lt $BB

echo $?

Judgment 1 | | Judgment 2

[-F/ETC/PASSWD] Determine if the file exists

echo $?

0

[-R/ETC/PASSWD]

echo $?

0

[Email protected] scripts]# aa=3 bb=4[[email protected] scripts]# [$aa-lt $BB] && echo okok[[email protected] scripts]# [$aa-gt $bb] && echo OK [[email protected] scripts]# [$aa-gt $BB] | | echo OK OK

Judging the Code

[[email protected] scripts]# vim if.sh#!/bin/bashgrep-q ^$1/etc/passwdif ["$?"-eq 0]; Then echo "$ is exist" else echo "exist" fi

If...then

[Email protected] scripts]# vim age.sh#!/bin/bashread-p "Please input your Age:" Ageif ["$age"-le 0] | | ["$age"-ge 100]; then echo "Please input correct age" Read Ageelif ["$age"-GT 0] && ["$age"-lt 20]; Then echo "Junior" Elif ["$age"-ge] && ["$age"-lt 50]; Then echo "You is adult" else echo "You is old" fi

While loop

[Email protected] scripts]# vim sum.sh#!/bin/bashsum=0while [$sum-lt]do let Sum+=1 echo $sumdone
#!/bin/bashread-p "Please input your name:" Namewhile ["$name"! = Tom] do echo "Please input correct name" Read namedone~


[[email protected] scripts]# vim case.sh#!/bin/bashxx=0until [  "$xx"  -gt  24 ]do        case  "$xx"  in                 [0-9]|1[01])                           echo  "Good morning"                          ;;                 12)                           echo  "It ' S lunch time"                          ;;                 1[3-7])                           echo  "Good afternoon"                          ;;                 *)                           echo  "Good evening"         esac          let xx+=1done


Learn shell scripts

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.