Shell Simple Script Writing

Source: Internet
Author: User
Tags case statement

vim/root/7.sh

#!/bin/bash

X=$[RANDOM%10+1]

Read-p "[1-10] you guessed:" Cai

If [$cai-eq $X];then

Echo, congratulations on your guess.

Exit

elif [$num-gt $X];then

Echo's a big guess.

Else

Echo's too small.

Fi


vim/root/6.sh

#!/bin/bash

Read-p "Please enter a package:" B

b=$ (Rpm-qa | grep "$a")

if [[-Z $b]];then

echo "Not Installed"

Else

echo "Already Installed"

Fi


vim/root/5.sh

#!/bin/bash

Netstat-anptu | grep httpd &>/dev/null

If [$?-eq 0];then

echo "Already started"

Else

echo "No Boot"

Fi

vim/root/4.sh

#!/bin/bash

PING-C3-I0.2-W1 $ &>/dev/null

If [$?-eq 0];then

echo "Host" Up "

Else

echo "Host" Down "

Fi


vim/root/3.sh

#!/bin/bash

[-d/mnt] | | Mkdir/mnt

Mount/dev/cdrom/mnt


vim/root/2.sh


#!/bin/bash

[!-d/mnt] && mkdir/mnt

Mount/dev/cdrom/mnt


vim/root/1.sh

#!/bin/bash

num=$ (who | wc-l)

[$num-gt] && mail-s Errot Root < Mail.txt


Crontab-e


* * * * * */root/1.sh

Vim/mail.txt

The Old king is coming next door.



Ping 176.130.8.50-150, how many are open, how many are not open


#!/bin/bash

X=0

Y=0

For i in {100..150}

Do

Ping-c2-i0.1-w1 176.130.8. $i &>/dev/null

If [$?-eq 0];then

echo "176.130.8. $i is Up"

Let X + +

Else

echo "176.130.8. $i is Down"

Let y++

Fi

Done

echo "Power on the $x, shut down the $y have a"



Seeking 1+2+3+4+5......100 and

#!/bin/bash

Sum=0

For i in {1..100}

Do

Let Sum+=i

Done

echo "Sum $sum"

Find the product of 1*2*3*4......30

#!/bin/bash

Sum=1

For i in {1..50}

Do

Let Sum\*=i

Done

echo "A total of $sum"


/var/log each file, individually packaged, and saved to/TMP


For i in ' Ls/var/log '

Do

tar-czf/tmp/$i. tar.gz/var/log/$i

Done


----------Assist in understanding the packaging process of/var/log/file understanding

For i in A.txt b.txt c.txt d.txt

Do

I=a.txt

tar-czf/tmp/$i. tar.gz/var/log/$i

Done


Using a shell script, guess a random number

#!/bin/bash

Y=0

NUM=$[RANDOM%100+1]

While:

Do

Read-p "[1-100], you guessed a number:" Cai

Let y++

If [$cai-eq $num];then

echo "Kong, you guessed $y times."

Exit

elif [$cai-gt $num];then

echo "Guess big."

Else

echo "small guess."

Fi

Done


Case statement

If the parameter is Redhat, the display is Fedora

If the over parameter is fedora, the display is Redhat

If the parameter is other, the display is useage


#!/bin/bash

Case "$" in

Redhat

echo "Fedora";;

Fedora

echo "Redhat";;

*)

echo "Usage";;

Esac


Case statement (Judgment statement), simplified if statement-----------only two values are equal

#!/bin/bash

Case "$" in

-N)

Touch $;;

-e)

Vim $;;

-c)

Cat $ n;;

-R)

RM-RF;;

*)

echo "Usage:$0 (-n|-e|-c|-r|)" File name

Esac


The shell calls the function----------is to give a command to the individual name (function)

Basic format: Function name () {

Curly braces Middle with command

}


Calling functions: Function names

LL (Ls-l)


#!/bin/bash

Cecho () {

Echo-e "\033[$1m$2\033[0m"

}

Cecho-OK

Cecho Error

Cecho Waring

3 x: is the font color

4 x: is the background color

X: Represents a Style




While statement


Fixed format: While


I=1

While [$i-lt 10]

Do

echo "Hello"

Let i++

Done



Break,contine,exit indirect interrupts to scripts are reused

(1) Break interrupts the entire loop body

(2) Continue interrupt this cycle, immediately jump into the next cycle

(3) Exit interrupts the entire script


#!/bin/bash

For i in {1..5}

Do

[$i-eq 3] && break

Echo $i

Done

Echo OK


#!/bin/bash

For i in {1..5}

Do

[$i-eq 3] && continue

Echo $i

Done

Echo OK


#!/bin/bash

For i in {1..5}

Do

[$i-eq 3] && exit

Echo $i

Done

Echo OK


#!/bin/bash

Sum=0

While:

Do

Read-p "Please enter Number [0 end]:" XX

[$xx-eq 0] && break

Let Sum+=xx

Done

echo "Sum is $sum"


Print 9*9 multiplication table


#!/bin/bash

For i in ' seq 9 '

Do

For x in ' seq $i '

Do

Echo-n "$i * $x =$[$i * $x]"

Done

Echo

Done


Monkey picking Banana

#!/bin/bash

X=1

For i in {1..8}

Do

Let X=x+1

Let X=x*2

Done

Echo $x


Chess


#!/bin/bash

B=1

While [$b-le 4]

Do

For i in {1..4}

Do

Echo-en "\033[40m \033[0m"

Echo-en "\033[47m \033[0m"

Done

Echo

For i in {1..4}

Do

Echo-en "\033[47m \033[0m"

Echo-en "\033[40m \033[0m"

Done

Echo

If [$b-eq 2];then

Echo-e "\033[47m Chu Chu bounded \033[0m"

Fi

Let b++

Done


#!/bin/bash

Read-p "Please enter user name:" Name

Read-p "Please enter user password:" Pass

If [-Z ' $name '];then

echo "must enter user name"

Exit

Fi

pass=${pass:-123456}

Useradd $name

echo "$pass" | passwd--stdin $name


Prompts the user to enter a number: If the input is 10, the script 1+2+3+4.....10

If the input is 20, the script 1+2+3....+20

If there is no input, the script 1+2+3+4......+100

#!/bin/bash

Read-p "Please enter a number:" XX

XX=${XX:-100}

Sum=0

For i in ' seq $xx '

Do

Let Sum+=i

Done

echo "Sum is $sum"

Create file Touch {a,b,c,d}.txt, change the. txt suffix to. doc

#!/bin/bash

For i in ' LS *.txt '

Do

MV $i ${i%.txt}.txt

Done

Change parameters, assign variables

#!/bin/bash

For i in ' LS *.$1 '

Do

MV $i ${i%.$1}.$2

Done


#!/bin/bash

Sum=0

For i in ' SEQ 2 2 100 '

Do

Let Sum=sum+i

Done

echo "$sum"


Sum=0

For i in ' SEQ 1 2 99 '

Do

Let Sum+=i

Done

echo "Sum is $sum"


#tmp =0

Read-p "Shu zi" num

NUM=${NUM:-100}

For i in ' SEQ 2 2 $num '

Do

Let Tmp+=i

Done

Echo $tmp


#!/bin/bash

Case $ in

Start

if [-f/usr/local/nginx/logs/nginx.pid];then

echo "Running"

Else

/usr/local/nginx/sbin/nginx

echo "started successfully"

fi;;

Stop

if [!-f/usr/local/nginx/logs/nginx.pid];then

echo "Closed"

Else

/usr/local/nginx/sbin/nginx-s stop

echo "Closed successfully"

fi;;

Restart

if [-f/usr/local/nginx/logs/nginx.pid];then

/usr/local/nginx/sbin/nginx-s stop

/usr/local/nginx/sbin/nginx

echo "Reboot succeeded"

Else

/usr/local/nginx/sbin/nginx

echo "Reboot succeeded"

fi;;

Status

if [-f/usr/local/nginx/logs/nginx.pid];then

Netstat-ntulp |grep Nginx

Else

echo "Not Running"

fi;;

*)

echo "Error";;

Esac


Shell Simple Scripting

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.