首页 > IT职业 > IT职场 > 详解Linux Shell(函数与sed)
$1、$2...$n
#!/bin/bash
this_pid=$$
status=`ps -ef | grep nginx | grep-v grep | grep -v $this_pid &> /dev/null`
if [$status == 0];then
echo "Nginx is running well"
sleep 60
else
systemc start nginx
echo "Nginx is down,Start it...."
fi
向函数传递参数:
函数传参和给脚本传参类似,都是使用$1$2$3$4$5$6$7这种方式
#!/bin/bash
function calcu
{
case $2 in
+)
echo "expr $1+$3"
;;
-)
echo "expr$1-$3"I
;;
*)
echo "expr $1*$3"
;;
/)
echo "expr $1/$3"
;;
esac
}
calcu $1 $2 $3
#!/bin/bash
this_pid=$$
function is_nginx_running
{
ps-ef | grep nginx | grep -v grep | grep-v Sthis_pid s>/dev/null
if[ $? -eq 0];then
return
else
return 1
fi
}
if_nginx_ running && echo "Nginx is running " || echo "Nginx is stoped"
#!/bin/bash
function get users
{
users=`cat /etc/passwd | cut -d: -f1`
echo $users
}
user_list=`get_users`
index=1
for u in $user_list
do
echo "The $index user is:$u"
index=$(($index+1))
done
function sys load
{
echo "Memory Info"
echo
free-m
echo
echo "Disk Usage"I
echo
df-h
echo
}
#prune通常和-path一起使用,用于将特定目录排除在搜索条件之外
#例子1:查找当前目录下所有普通文件,但排除test目录
find . -path ./etc -prune -o -type f
#例子2:查找当前目录下所有普通文件,但排除etc和opt目录
find .-path./etc -prune -o -path./opt -prune-o-type f
#例子3:查找当前目录下所有普通文件,但排除etc和opt目录,但属主为hdfs
find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs
#例子4:查找当前目录下所有普通文件,但排除etc和opt目录,但属主为hdfs,且文件大小必须大于500字
find . -path ./etc -prune -o -path ./opt -prune -o-type f -a -user hdfs -a -size +500c
find ./etc/ -type f-name '*.conf' -size +10k -exec rm -f {} ;find /var/log/ -name '*.log' -mtime +7-exec rm -rf {} ;find ./etc/ -size +10k -type f -name '*.conf' -exec cp {} /root/conf/ ;例子1:查找当前目录下,属主不是hdfs的所有文件
find . -not -user hdfs | find . ! -user hdfs
例子2:查找当前目录下,属主属于hdfs,且大小大于300字节的文件
find . -type f -a -user hdfs -a -size +300c
例子3:查找当前目录下的属主为hdfs或者以xm1结尾的普通文件
find . -type f -a (-user hdfs -o -name '*.xml')
作用:仅查找二进制程序文件
-b 只返回二进制文件
sed -n "17p" fi1e #打印file文件的第17行
sed -n "10,20p" file #打印file文件的10到20行
sed -n "10,+5p" file #打印file文件中从第10行开始,往后面加5行的所有行
sed -n "/^root/p" file #打印fi1e文件中以root开头的行
sed -n "/^ftp/,/^mail/p" file #打印fi1e文件中第一个匹配到以ftp开头的行,到第二个以mail开头的行
sed -n "4,/^hdfs/p" file #打印fi1e文件中从第4行开始匹配,直到以hdfs开头
sed -n "/root/,10p" file #打印fi1e文件中匹配root的行,直到第10行结束
例如:如果我们仅想要替换匹配到的字符串的一部分,name必须使用**1**这种方式,不能
查找test.txt文件中以1开头,紧接着跟两个任意字符,再接一个e的字符串。将找到的字符串中开头的文字替换成L
sed "s/1(..e)/L1/g" test.txt
#1、打印/etc/passwd中第20行的内容
sed -n '20p' /etc/passwd
#2、打印/etc/passwd中从第8行开始,到第15行结束的内容
sed -n '8,15p' /etc/passwd
#3、打印/etc/passwd中从第8行开始,然后+5行结束的内容
sed -n '8,+5p' /etc/passwd
#4、打印/etc/passwd中开头匹配hdfs字符串的内容
sed -n '/^hdfs/p' /etc/passwd
#5、打印/etc/passwd中开头为root的行开始,到开头为hdfs的行结束的内容
sed -n '/^root/,/^hdfs/p' /etc/passwd
#6、打印/etc/passwd中第8行开始,到含有/sbin/nologin的内容的行结束内容
sed -n '8,//sbin/nologin/p' /etc/passwd
#7、打印/etc/passwd中第一个包含/bin/bash内容的行开始,到第5行结束的内容
sed -n '//bin/bash/,5p' /etc/passwd
#!/bin/bash
FILE NAME=/root/lesson/5.6/my.cnf
function get_all_segments
{
echo "`sed -n '/[.*]/p' my.cnf | sed -e 's/[//g'-e's/]//g'`"
}
for seg in get all segments
echo " 配置项:$seg"
done
function count_items_in_segment
{
items=`sed -n '/['$1']/,/[.*]/p' $FILE_NAME | grep -v "^#" | grep -v "^$" | grep -v "[.*]"`
index = 0
for item in $items
do
index=`expr $index+1`
done
echo $index
}
number=0
for segment in `get_all_segements`
do
number = `expr $number+1`
items_count=`count_items_in_segment $segment`
echo "$number: $segment $items_count"
done
#1、删除/etc/passwd中的第15行
sed -i '15d' /etc/passwd
#2、删除/etc/passwd中的第8行到第14行的所有内容
sed- i '8,14d' passwd
#3、删除/etc/passwd中的不能登录的用户(筛选条件:/sbin/nologin)
sed -i '//sbin/nologin/d' passwd
#4、删除/etc/passwd中以mai1开头的行,到以yarn开头的行的所有内容
sed -i '/^mail/,/^yarn/d' passwd
#5、删除/etc/passwd中第一个不能登录的用户,到第13行的所有内容
sed -i '//sbin/nologin/,13d' passwd
#6、删除/etc/passwd中第5行到以ftp开头的所有行的内容
sed -i '5,/^ftp/d' passwd
#7、删除/etc/passwd中以yarn开头的行到最后行的所有内容
sed -i '/^yarn/,$' passwd
#8、删除配置文件中的所有注释行和空行
sed -i '/[:blank:]*#/d;/^$/d' nginx.conf
#9、在配置文件中所有不以#开头的行前面添加*符号,注意:以#开头的行不添加
sed -i 's/^[#]/*&/g' nginx.conf
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3643/viewspace-2823047/ ,如需转载,请注明出处,否则将追究法律责任。