gcc环境配置
$ sudo apt-get install build-essential
shell编程
1.helloworld.sh
$ vim helloworld.sh
#!/bin/bash echo "helloworld" var="hello var" //定义变量var 变量赋值等号两边不能有空格 echo $var //输出变量,$+变量名 for file in $(ls /home/path/); do //for循环输出 echo "${file}" done
为脚本添加可执行权限
$ chmod +x helloworld.sh
执行脚本
$ ./helloworld.sh
2.累加1到100的和
#!/bin/bash sum=0 for i in {1..100};do let sum+=i done echo $sum
3.ping局域网ip地址
#!/bin/bash for i in {1..254};do ping -c 2 -i 0.5 192.168.180.$i &> /dev/null //&> 重定向输出到/dev/null if [ $? -eq 0 ];then echo "192.168.180.&i is up" else echo "192.168.180.&i is down" fi done
4.脚本获取本机ip地址
#!/bin/bash str=$(ifconfig) //笨方法1 echo ${#str} echo ${str:78:15} //使用grep和awk从ifconfig输出中提取IPv4地址 ip_address=$(ifconfig ens33 | grep 'inet ' | awk '{print $2}') echo "IP Address: $ip_address"