shell简单实例

shell简单实例

这是我看到的一个帖子觉得不错,很入门就保存下来了. 贴主说的很对,所有的大的应用都是由小的模块对记起来的.

模拟linux登录

1
2
3
4
5
6
7
8
9
#!/bin/bash
echo -n "login:"
read name
echo -n "password:"
read passwd
if [ $name = "cht" -a $passwd = "abc" ]; then
echo "the host and password is reight!"
else echo "input is error!"
fi

比较两个数大小

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
echo "please enter two number"
read a
read b
if test $a -eq $b
then echo "NO.1 = NO.2"
elif test $a -gt $b
then echo "NO.1 > NO.2"
else echo "NO.1 < NO.2"
fi

查找/root/目录下是否存在该文件

1
2
3
4
5
6
7
#!/bin/bash
echo "enter a file name:"
read a
if test -e /root/$a
then echo "the file is exist!"
else echo "the file is not exist!"
fi

for循环的使用

1
2
3
4
5
6
#!/bin/bash
clear
for num in 1 2 3 4 5 6 7 8 9 10
do
echo "$num"
done

查看是否当前用户

1
2
3
4
5
6
7
8
#!/bin/bash
echo "Please enter a user:"
read a
b=$(whoami)
if test $a = $b
then echo "the user is running."
else echo "the user is not running."
fi

删除当前目录下大小为0的文件

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
for filename in `ls`
do
if test -d $filename
then b=0
else
a=$(ls -l $filename | awk '{ print $5 }')
if test $a -eq 0
then rm $filename
fi
fi
done

如果/export/um_lpp_source下有文件,那么将其文件系统大小改为3G

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
while line=`ls /export/um_lpp_source`
do
if test $line=""
then echo "NULL"
sleep 1
else echo $line
chfs -a size=3G /export/um_lpp_source
exit 0
fi
done

测试IP地址

1
2
3
4
5
6
#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "the numbner of $i computer is"
ping -c 192.168.0.$i
done

如果test.log的大小大于0,那么将/opt目录下的*.tar.gz文件拷贝到当前目录下

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
a=2
while name="test.log"
do
sleep 1
b=$(ls -l $name | awk '{print $5}')
if test $b -gt $a
then `cp /opt/*.tar.gz .`
exit 0
fi
done

打印读取的内容

1
2
3
4
5
#!/bin/bash
while read name
do
echo $name
done

从0.sh中读取内容并打印

1
2
3
4
5
#!/bin/bash
while read line
do
echo $line
done < 0.sh

读取a.c中的内容并做加1运算

1
2
3
4
5
6
7
#!/bin/bash
test -e a.c
while read line
do
a=$(($line+1))
echo $a
done < a.c

普通无参数函数

1
2
3
4
5
#!/bin/bash
p(){
echo "hello"
}
p

给函数传递参数

1
2
3
4
5
6
7
8
9
#!/bin/bash
p_num(){
num=$1
echo $num
}
for n in $@
do
p_num $n
done

创建文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
while :
do
echo "please input file's name"
read a
if test -e /root/$a
then
echo "the file is existing Please input new file name:"
else
mkdir $a
echo "you are sucessfull"
break
fi
done

获取本机IP地址

1
2
#!/bin/bash
ifconfig | grep "inet addr:" | awk '{print $2}'| sed 's/addr://g'

查找最大文件

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
a=0
for name in *.*
do
b=$(ls -l $name | awk '{print $5}')
if test $b -gt $a
then a=$b
namemax=$name
fi
done
echo "the max file is $namemax"

查找当前网段内IP用户,重定向到IP.txt文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash  
a=1
while :
do
a=$(($a+1))
if test $a -gt 255
then break
else
echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
echo $ip >> ip.txt
fi
done

打印当前用户

1
2
3
#!/bin/bash
echo "Current User is : "
echo $(ps | grep "$$" | awk '{print $2}')

case语句练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
clear
echo "enter a number from 1 to 5:"
read num
case $num in
1) echo "you enter 1"
;;
2) echo "you enter 2"
;;
3) echo "you enter 3"
;;
4) echo "you enter 4"
;;
5) echo "you enter 5"
;;
6) echo "you enter 6"
;;
7) echo "you enter 7"
;;
8) echo "you enter 8"
;;
9) echo "you enter 9"
;;
0) echo "you enter 0"
esac

yes/no返回不同的结构

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
clear
echo "enter [y/n]"
read a
case $a in
y|Y|Yes|YES) echo "you enter $a"
;;
n|N|NO|no) echo "you enter $a"
;;
*) echo "error"
;;
esac

内置命令的使用

1
2
3
4
5
6
7
8
#!/bin/bash
clear
echo "Hello, $USER"
echo "Today's date id `date`"
echo "the user is :"
who
echo "this is `uname -s`"
echo "that's all folksl "

打印无密码用户

1
2
3
#!/bin/bash
echo "No Password User are :"
echo $(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print $1}')

加个自己的启动脚本-start

1
java -jar -server -Xms500m -Xmx500m -Xss256k -XX:+UseG1GC -XX:MaxGCPauseMillis=80 -XX:ParallelGCThreads=20 -XX:ConcGCThreads=8 -XX:-ResizePLAB -XX:InitiatingHeapOccupancyPercent=45 -XX:+UseFastAccessorMethods -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8062 -Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -XX:+AlwaysPreTouch -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintTLAB -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:logs/gc`date +%Y%m%d%H%M%S`.log projectName.jar > logs/out.log 2>&1 &

加个自己的启动脚本-stop

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
echo "-----start-------"
ID=`ps -ef | grep projectName | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
echo $ID
for id in $ID
do
kill -9 $id
echo "killed $id"
done
echo "------end-------"

加个自己的启动脚本-restart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
echo "-----stop-start-------"
ID=`ps -ef | grep projectName | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
echo $ID
for id in $ID
do
kill -9 $id
echo "killed $id"
done
echo "------stop-end-------"
echo "-------start--------"
echo "starting..."
./start.sh
echo "-------end----------"