Introduction to Shell Process statement usage

Source: Internet
Author: User

1) Use if, case, read
Example 1:
#!/bin/bash
#读取终端输入的字符
Read-p "Please input a number:" N
N1= ' echo $n |sed ' s/[0-9]//g '

#使用if语句进行判断
if [!-Z $n 1]
Then
echo "Please input a number."
Exit 1
elif [$n-lt 0] | | [$n-GT 100]
Then
echo "The number Ranger is 0-100"
Exit 1
Fi

n2=$[$n%2]

#使用case语句进行判断
Case $n 2 in
0)
echo "Even"
;;
1)
echo "Odd"
;;
*)
echo "Please input a number."
;;
Esac

Example 2:
#!/bin/bash
Read-p "Please input a digit:" N
#n2 = ' echo $n |grep-c ' [^0-9] '
#n1 =$[$n%2]

#if [$n 2-eq 1]
If echo $n | Grep-q ' [^0-9] '
Then
echo "You entered is not a number, please re-enter"
Exit 1
Fi


n1=$[$n%2]

If [$n 1-eq 0]
#if (($n 1==0))
Then
echo "Input is even"
Else
echo "Input is cardinality"
Fi

Example 3:
#!/bin/bash
A=1
if [$a = = 2]
Then
echo "true"
elif [$a-lt 10]
Then
echo "No false"
Else
echo "false"
Fi

Example 4:
#!/bin/bash

files=$*
If [-Z $*];
Then
Echo-e "\033[32m{usage:$0/boot|/tmp|/tmp/test.txt}\033[0m"
Exit
Fi

For i in ' echo 192.168.204.129 127.0.0.1 '
Do

Scp-r $FILES [email protected] $i:/root/install

Done

2) Use Date
#!/bin/bash
d= ' Date +%f '
exec > $d. log 2>&1
echo "Begin at ' date '"
Ls/learing
Cd/log
Ls/root >root.log
echo "End at ' date '"

3) Use the FOR
Example 1:
!/bin/bash
Sum=0
#打印1到100相加后的总和
For i in ' SEQ 1 100 '
Do
sum=$[$sum + $i]
Echo $i
Done
Echo $sum

Example 2:
!/bin/bash
For f in ' ls/etc/'
Do
If [-d/etc/$f]
Then
ls-d/etc/$f
#echo "/etc/$f"
Fi
Done

Example 3:
#!/bin/bash
N= ' wc-l passwd |awk ' {print $} '
For i in ' seq 1 $n ';
Do
Sed-n "$i" P passwd;
Done

Example 4:
#!/bin/bash

#while Read Line
For i in ' Cat ip.txt '
Do
Echo-e "\033[32m scp-r/root/learing/t8.sh [email protected] $i:/tmp \033[0m"
Done

Example 5:
#!/bin/bash
Pfile= ' Find. -name "*.sh" |tail-2 '
For i in ' $pfile '
Do
TAR-CZVF 2016-07-22.tar.gz $i
Done

Example 6:
#!/bin/bash
J=0
For ((i=1;i<=100;i++))
Do
j=$[$i + $j]
Done
Echo $j

4) using the while
Example 1:
#!/bin/bash
I=0
#while [$i-lt 5]
while [[$i <5]]
Do
echo "$i"
((i++))
Done

Example 2:
#!/bin/bash
While:
Do
Read-p "Please enter:" N
If [-Z $n]
Then
echo "needs to be entered:"
Continue
Fi

N1= ' echo $n |sed ' s/[0-9]//g '

if [!-Z $n 1]
Then
echo "Need to enter a number:"
Continue
Fi

Break
Done
Echo $n

Example 3:
#!/bin/bash
While:
Do
load= ' w|head-1|awk-f ' load average: ' {print $ {} ' |cut-d '. '-f1 '
If [$load-GT 10]
Then
Top|mail-s "Load is High: $load" [email protected]
Fi
Sleep 30
Done

Example 4:
#!/bin/bash
While Read line
Do
Echo-e "\033[32m scp-r/root/learing/t8.sh [email protected] $line:/tmp \033[0m"
Done < Ip.txt

Example 5:
#!/bin/bash
While Read line
Do
Echo $line
Done </etc/hosts

5) calling a custom function
#!/bin/bash
IPAddress ()
{
Ifconfig |grep-a1 "$" |tail-1|awk ' {print $} ' |awk-f ': ' {print $} '
}

Read-p "Please enter the network card name:" Ipname
myip= ' IPAddress $ipname '
echo "$ipname address is $myip"

6) Use multiple commands
Example 1:

#!/bin/bash

Date= ' Date '
echo "DATE is ${date}"

Users= ' Who |wc-l '
echo "LOGIN in User is ${users}"


up= ' Date;uptime '
echo "Uptime is ${up}"

Example 2:

#!/bin/bash
#机器台数
machinenum= ' ifconfig |grep-n ' ^[a-za-z0-9] ' |wc-l '

#获取机器的机器名所在行和机器名
Machinename= ' ifconfig |grep-n ' Link encap ' |awk-f ' {print $} ' |awk-f ': ' {print $} '

#获取机器的机器名对应的IP地址
machineip= ' ifconfig |grep-n ' inet addr ' |awk-f ': ' {print $} ' |awk-f ' ' {print '} '

For num in ' seq 1 $machineNum ';
Do
# ' echo ' $machineName ' |sed-n ' $num ' P '
N= ' echo ' $machineName ' |sed-n ' $num ' P '
M= ' echo ' $machineIP ' |sed-n ' $num ' P '
echo "Machine $num: $n IP address is: $m";
Done

7) Read the contents of the file
#!/bin/bash
For IP in ' cat ip.txt '
Do
Echo $ip
./rsync.expect $ip Ip.txt
Done

Introduction to Shell Process statement usage

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.