最近下定决心,一定要掌握基本的shell,在linux下哪有不会shell的?(我看估计很少,很不行,我就是其中一个 -_-!!! )
刚开始的话,太难的一时半会儿肯定也啃不下去,在应用中掌握嘛(个人看法~)我觉得很多论坛都是很好的学习地方!
原则就是多看,多思考,多动手,多搜索,多问!只要掌握这五个”多”,相信一段日子后我们也会成为帮助别人的人~
今天入门写了个简单得不能再简单的shell程序,其作用判断当前的路径,也就是linux下的pwd命令。其实别看很简单,知识就是一点一滴这样掌握下来的,没有人天生就会干什么~
- #!/bin/sh
- a=$(pwd)
- if [ "$a" = "/root" ]
- then
- echo "you are in the root directory"
- else
- echo "you are in the "$a" directory"
- fi
- root@debian:~# ./test/where.sh #在root路径运行
- you are in the root directory
- root@debian:~/test# ./where.sh #在/root/test路径下运行
- you are in the /root/test directory
shell编程入门_find命令查找字符设备文件
脚本的功能在于判断当前目录下,输入的文件名是否是字符设备文件,如果是,拷贝去ccc目录,如果不是就放弃这个命令。find命令的 -type有c选项,c就是字符设备文件。
- #!/bin/sh
- echo "请输入文件名: "
- read input
- if [ -z $input ]
- then
- echo "文件名无效!"
- else
- ls -l $input|grep '^c' && cp -a $input /root/ccc || echo "非字符设备文件!放弃命令!"
- fi
在读shell十三问~网中人netman兄的大作~通俗易懂,争取尽快啃下来。
写个简单的脚本,是系统的ping命令,很感谢cu论坛上的DBA~
- #!/bin/sh
- echo -n '请输入域名/IP : '
- read input
- if [ -z $input ]
- then
- echo "输入非域名或IP!"
- else
- ping $input -c 3
- fi
保留一下以备查询~在shell里if的判断
- -eq 等于
- -ne 不等于
- -gt 大于
- -lt 小于
- -le 小于等于
- -ge 大于等于
- -z 空串
- = 两个字符相等
- != 两个字符不等
- -n 非空串
脚本虽简单,但是弄明白的地方有
shell里的if要以fi结束
echo 默认是换行的,不换行的话 -n
ifconfig eth0|grep “inet addr:”|awk ‘{print $2}’|sed ’s/addr://g’|xargs ping -c 3
截取ifconfig的结果,用到grep、awk跟sed,xargs把结果传给ping命令。
再接再厉!