Linux初学实战之shell
来源:互联网 发布:esp8266与单片机连接 编辑:程序博客网 时间:2024/05/18 00:26
初学简单shell script,实践即可懂!
1,带参数输入
echo "The name of the script is :"$0echo "the first parameter is:"$1执行
chmod +x 1_echo.shbash 1_echo.sh hello结果
The name of the script is :1_echo.shthe first parameter is:hello2,判断文件是否存在
file=$1if [ -f $file ]thenecho -e "The $file exist!"elseecho -e "The $file does not exist!"fi3,相加
echo "Please enter the first number:"read input1echo "Please enter the second number:"read input2echo "The first number add the second number is:" $((input1 + input2))执行
chmod +x 3_add.shbash 3_add.sh结果
Please enter the first number:4Please enter the second number:4The first number add the second number is: 84,比较
echo "------------start--------------"echo "Please input the first number :"read input1echo "Please input the second number:"read input2if [ $input1 -eq $input2 ]then echo "input1 equal to input2"elif [ $input1 -gt $input2 ]then echo "input1 is greater than input2"else echo "input1 is smaller then input2"fiecho "-------------end-------------"执行
chmod +x 4_compare.shbash 4_compare.sh结果
------------start--------------Please input the first number :1Please input the second number:4input1 is smaller then input2-------------end-------------
5 ip
echo "Please enter a IP adress"read ipif [ ! -z $ip ]then ping -c 1 $ip if [ $? -eq 0 ] then echo "Machine is giving ping response" else echo "Machine is not pinging" fielse echo "IP Address is empty"fi执行
chmod +x 5_ip.shbash 5_ip.sh结果
Please enter a IP adress210.72.131.130PING 210.72.131.130 (210.72.131.130) 56(84) bytes of data.64 bytes from 210.72.131.130: icmp_seq=1 ttl=62 time=4.64 ms--- 210.72.131.130 ping statistics ---1 packets transmitted, 1 received, 0% packet loss, time 0msrtt min/avg/max/mdev = 4.645/4.645/4.645/0.000 msMachine is giving ping response
6,string length
echo "please enter a string:"read strlength=${#str}echo the length is : $length执行
chmod +x 6_str_len.sh
bash 6_str_len.sh
结果
please enter a string:stringthe length is : 67, while
echo "enter a absolute path of a file name"read filenameexec <$filenamewhile read linedoecho $linedonenumber=0while [ $number -lt 10 ]do echo "number = $number" number=$((number+1))done执行
chmod +x 7_print.shbash 7_print.sh
结果
enter a absolute path of a file name/etc/profile# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))......number = 0number = 1number = 2number = 3number = 4number = 5number = 6number = 7number = 8number = 9
0 0
- Linux初学实战之shell
- Linux初学实战之Makefile
- Linux初学实战之return
- Linux 初学实战之环境配置
- Linux初学实战之管道piple
- Linux-shell初学(一)
- 初学Linux--shell
- Linux初学实战之重定向标准 I/O 流
- linux shell实战 笔记学习及学习之什么是shell
- linux初学之6——shell基础
- linux-shell脚本实战
- linux 初学之命令行
- 初学linux之笔记
- Linux之初学命令
- Linux Shell 编程实战技巧
- Linux Shell 编程实战技巧
- Linux Shell 编程实战技巧
- Linux Shell 编程实战技巧
- JDK的配置及出现问题的解决方案
- 安卓三种动画小结
- html中的js获取当前的页面是位于哪个容器中打开(pc,手机,微信...)
- 黑马程序员--线程间的通信
- igrimace v8 一键新机 ios8 抹机 平安易贷 微信陌陌站街 借贷宝
- Linux初学实战之shell
- 广播收到信息传到activity更新fragment
- 《leetCode》:Search in Rotated Sorted Array
- Scala学习第十二天 Scala中的继承:超类的构造、重写字段、重写方法代码实战
- cursor的一些总结
- 贪心算法_乘船问题
- java关键字
- 解决spring使用动态代理类型转换失败的问题--java.lang.ClassCastException: com.sun.proxy.$Proxy$ cannot be cast to ...
- 折线分割平面