#!/bin/bash
Read-p "Input a dight:"
Echo $REPLY
Date= ' Date '
echo "DATE is ${date}"
Users= ' Who |wc-l '
echo "LOGIN in User is ${users}"
up= ' Date;uptime '
echo "Uptime is ${up}"
#echo "HELLO world!"
#echo "What's Your Name"
#read person
#echo "HELLO ${person}"
#name = "Huangtao"
#echo ${name}
Myurl= "http://www.baidu.com/"
#readonly Myurl
#myUrl = "http://www.sina.com.cn/"
#myUrl
#unset Myurl
#echo ${myurl}
echo "File Name:$0"
echo "1st Parameters:$1"
echo "2nd parameters:$2"
echo "values:$*"
echo "Values:[email protected]"
echo "pid:$$"
echo "Return value:$?"
echo "Total number paramters:$#"
a=10
Echo-e "Value A is ${a}\n"
echo "Value A is ${a}\n"
d= ' Date +%f '
exec > $d. log 2>&1
echo "Begin at ' date '"
Ls/learing
Cd/log
Ls/root >root.log
echo "End at ' date '"
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"
#机器台数
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
#if例子1
A=1
if [$a = = 2]
Then
echo "true"
elif [$a-lt 10]
Then
echo "No false"
Else
echo "false"
Fi
#if例子2
# > GT < LT
# >= ge <= le = = eq! = NE
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例子3
If [$n 1-eq 0]
#if (($n 1==0))
Then
echo "Input is even"
Else
echo "Input is cardinality"
Fi
#for循环例子1
#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
#for循环例子2
Pfile= ' Find. -name "*.sh" |tail-2 '
For i in ' $pfile '
Do
TAR-CZVF 2016-07-22.tar.gz $i
Done
#for循环例子3
J=0
For ((i=1;i<=100;i++))
Do
j= ' expr $i + $j '
Done
Echo $j
#for循环例子4
For IP in ' cat ip.txt '
Do
Echo $ip
./rsync.expect $ip Ip.txt
Done
#for循环例子5
Sum=0
For i in ' SEQ 1 100 '
Do
sum=$[$sum + $i]
Echo $i
Done
Echo $sum
#for循环例子6
For f in ' ls/etc/'
Do
If [-d/etc/$f]
Then
ls-d/etc/$f
#echo "/etc/$f"
Fi
Done
#for循环例子7
N= ' wc-l passwd |awk ' {print $} '
For i in ' seq 1 $n ';
Do
Sed-n "$i" P passwd;
Done
#if While Loop example
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
#while循环例子1
I=0
#while [$i-lt 5]
while [[$i <5]]
Do
echo "$i"
((i++))
Done
#while循环例子2
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
#while循环例子3
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
#while循环例子4
While Read line
Do
Echo-e "\033[32m scp-r/root/learing/t8.sh [email protected] $line:/tmp \033[0m"
Done < Ip.txt
#while循环例子5
While Read line
Do
Echo $line
Done </etc/hosts
#if Case Cycle Example 1
Read-p "Please input a number:" N
N1= ' echo $n |sed ' s/[0-9]//g '
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 $n 2 in
0)
echo "Even"
;;
1)
echo "Odd"
;;
*)
echo "Please input a number."
;;
Esac
Examples of Linux shell processes