您的位置:首页 > 运维架构 > Linux

linux学习的一些记录(原创)

2008-01-04 15:51 519 查看
1.whereis 和 which 的区别
which: 只在PATH环境变量中寻找文件
whereis: 在系统定义的目录中寻找

2.根据关键字查找man页
举例:apropos split 或者 man -k split

3.sed示例:
sed -e s/root/toor/g /etc/passwd > ~/test.out 替换 /etc/passwd中的root为toor输出到~/test.out
sed -e 's/root/toor/g; s/ftp/ptf/g' /etc/passwd 使用 -e 指定多个命令
sed -e 's/root/toor/g' -e ' s/ftp/ptf/g' /etc/passwd 同上
使用命令文件:
/* test.sed 开始,不包含本行 */
s/root/toor/g
s/ftp/ptf/g
/* test.sed 结束,不包含本行 */
指令:sed -f test.sed /etc/passwd

4.awk示例:
awk '{print $0}' /etc/passwd $0表示完整的输入记录
awk -F":" '{print $1}' /etc/passwd 打印第一列,以:为分隔符
awk -F":" '{print "username: "$1 "/t/t/t user id: "$3}' /etc/passwd 格式化并打印
使用命令文件:
/* test.awk 开始,不包含本行 */
BEGIN{
FS=":"
}
{printf "username: "$1 "/t/t/t user id: "$3"/n"}
END{
printf "all done processing /etc/passwd/n"
}
/* test.awk 结束,不包含本行 */
指令:awk -f test.awk /etc/passwd

5.shell脚本
1)特殊变量:
?前一个命令输出状态
0当前脚本名
1~9参数
2)范例1,使用if语句
#!/bin/bash

echo "guest the select color"
read COLOR

if [ $COLOR = "yellow" ]
then
echo "you are correct"
elif [ $COLOR = "blue" ]
then
echo "you are correct also"
fi

3)范例2,使用case语句
#!/bin/bash

case "$1" in
start)
echo "start......"
;;
stop)
echo "stop......"
;;
status)
echo "status......"
;;
*)
echo "usage: $0 {start | stop | status}"
;;
esac

4)范例3,使用迭代流程
#!/bin/bash

echo "guest color: red, blue or orange/n"
read COLOR

while [ $COLOR != "orange" ]
do
echo "incorrect, try again"
read COLOR
done

echo "correct"

5)使用双引号进行命令替换
lines="$(wc -l 3.sh)"
echo $lines

6)测试文件
-d file 目录
-e file 存在
-r file 可读
-w file 可写
-x file 可执行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: