linux shell 实用脚本
2016-06-29 09:28
471 查看
测试IP地址
#/bin/bash
for i in 1 2 3 4 5 6 7 8 9
do
echo "the number of $i computer is "
ping -c 5 192.168.0.$i #ping -c 5 是指ping 5次后停止。
done
/******************************************************************/
判断test.sh 的大于指定大小 删除 本目录下其他文件
#!/bin/bash
a=2
while name="test.sh"
do
sleep 1
b=$(ls -l $name | awk '{print $5}' )
if test $b -ge $a
then
`rm awktest`
exit 0
fi
done
/******************************************************************/
11.从0.sh中读取内容并打印
#/bin/bash
while read line
do
echo $line
done < 0.sh
/******************************************************************/
从1.sh中读取每一行加一打印
#!/bin/bash
test -e 1.sh
while read line
do
a=$(($line+1))-------》1.sh中包含非数字默认为0
#或#
a=`expr $line + 1` ----》1.sh中包含非数字报错
echo $a
done<1.sh
/******************************************************************/
#!/bin/bash
p_num()
{
echo "$1" #$1:传递给函数的第一个参数
}
for n in $@ #$@ :传递给脚本的所有参数
do
p_num $n
done
/******************************************************************/
#!/bin/bash
while :
do
echo " please input file's name"
read a
if test -e ~/shell/$a # -e :检测文件是否存在
then
echo "the file is existing please input new file name"
else
mkdir $a
echo "you are susscesful"
break
fi
done
/******************************************************************/
获取本机IP地址
#/bin/bash
ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'
/******************************************************************/
获取当前用户
#/bin/bash
echo "Current User is :"
echo $(ps | grep "$$" | awk '{print $2}')
/******************************************************************/
case 练习
#!/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"
;;
*) echo "error"
;;
esac
/******************************************************************/
内置命令练习
#/bin/bash
clear
echo "Hello, $USER"
echo
echo "Today 's date id `date`"
echo
echo "the user is :"
who
echo
echo "this is `uname -s`"
echo
echo "that's all folks! "
/******************************************************************/
检测指定端口是否有服务占用
#!/bin/bash
clear
n=1
echo "check XXX servers..."
while :
do
if test $n -gt 20
then
echo "XXX servers was falid"
break
fi
sleep 3
n=$(($n+1))
port=`netstat -antp | grep "0.0.0.0:80"`
if [ ${#port} -gt 3 ]; then
echo "XXX servers is started"
break
fi
done
/******************************************************************/
#/bin/bash
for i in 1 2 3 4 5 6 7 8 9
do
echo "the number of $i computer is "
ping -c 5 192.168.0.$i #ping -c 5 是指ping 5次后停止。
done
/******************************************************************/
判断test.sh 的大于指定大小 删除 本目录下其他文件
#!/bin/bash
a=2
while name="test.sh"
do
sleep 1
b=$(ls -l $name | awk '{print $5}' )
if test $b -ge $a
then
`rm awktest`
exit 0
fi
done
/******************************************************************/
11.从0.sh中读取内容并打印
#/bin/bash
while read line
do
echo $line
done < 0.sh
/******************************************************************/
从1.sh中读取每一行加一打印
#!/bin/bash
test -e 1.sh
while read line
do
a=$(($line+1))-------》1.sh中包含非数字默认为0
#或#
a=`expr $line + 1` ----》1.sh中包含非数字报错
echo $a
done<1.sh
/******************************************************************/
#!/bin/bash
p_num()
{
echo "$1" #$1:传递给函数的第一个参数
}
for n in $@ #$@ :传递给脚本的所有参数
do
p_num $n
done
/******************************************************************/
#!/bin/bash
while :
do
echo " please input file's name"
read a
if test -e ~/shell/$a # -e :检测文件是否存在
then
echo "the file is existing please input new file name"
else
mkdir $a
echo "you are susscesful"
break
fi
done
/******************************************************************/
获取本机IP地址
#/bin/bash
ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'
/******************************************************************/
获取当前用户
#/bin/bash
echo "Current User is :"
echo $(ps | grep "$$" | awk '{print $2}')
/******************************************************************/
case 练习
#!/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"
;;
*) echo "error"
;;
esac
/******************************************************************/
内置命令练习
#/bin/bash
clear
echo "Hello, $USER"
echo
echo "Today 's date id `date`"
echo
echo "the user is :"
who
echo
echo "this is `uname -s`"
echo
echo "that's all folks! "
/******************************************************************/
检测指定端口是否有服务占用
#!/bin/bash
clear
n=1
echo "check XXX servers..."
while :
do
if test $n -gt 20
then
echo "XXX servers was falid"
break
fi
sleep 3
n=$(($n+1))
port=`netstat -antp | grep "0.0.0.0:80"`
if [ ${#port} -gt 3 ]; then
echo "XXX servers is started"
break
fi
done
/******************************************************************/
相关文章推荐
- shell小脚本
- (OK)(OK) adb -s emulator-5554 shell - natpf1 - VirtualBox - NAT Port Forwarding
- shell 编程基础
- shell中的条件判断
- shell 获取系统时间
- 使用shell和python分别实现简单菜单功能--打印当前系统状态信息
- XShell 快捷键
- SHELL--待续
- shell中的if-then语句
- SHELL实现CentOS6,Ubuntu16下自动下载,编译安装LAMP
- Linux:-bash: ***: command not found
- Android之adb shell dumpsys activity获取task里面的所有actitiy
- sed的工作原理及使用
- Shell 循环语句:if,case esac,for,while
- 如何在C语言中调用shell命令
- GitBash+GitLab+Eclipse使用攻略非常详细版
- Kaldi学习手记(三): Kaldi 的I/O机制
- shell脚本 sed工具练习
- Shell 注释,echo,printf
- java 调用linux shell脚本