Linux shell script
来源:互联网 发布:怎么查看电脑的端口 编辑:程序博客网 时间:2024/06/05 13:29
这里主要记载工作中用到的一些小脚本:(ps 我每次都会把最新写的放到第一位)
*、输入一个日期,可以知道此日期的上周四是几号
#!/bin/bashDATA=$1date -d $DATA +%w | awk '{print $0+3}' |xargs -i date -d "$DATA {} days ago" +%Y%m%d# 首先 $1 是个位置变量,+%w 可以得出此日期是星期几,我们可以找到规律:周四到周日是距 3 天,这个不会变,我们把周四到周末加上星期几正好是 $1 到上周四的天数,然后指定 $1 的前 { 此为 $0+3 } 天,然后按格式 +%Y%m%d 就可以得出结果。[root@localhost tmp]# ./a.sh 2014092020140911 # 可以看到 9 月 20 号的上周四是 9 月 11 号
*、使文件中的两个字段相乘
[root@gameserver ~]# head -20 /var/tmp/1.csv # 需要处理的文件 实际有 9181 行::ffff:1.0.32.0,115,1809858,23.1167,113.25::ffff:1.0.8.0,117,1809858,23.1167,113.25::ffff:1.0.2.0,119,1814991,35,105::ffff:1.0.1.0,120,1814991,35,105::ffff:1.1.32.0,115,1809858,23.1167,113.25::ffff:1.1.16.0,116,1809858,23.1167,113.25::ffff:1.1.8.0,117,1809858,23.1167,113.25::ffff:1.1.4.0,118,1810821,26.0614,119.3061::ffff:1.1.2.0,119,1814991,35,105::ffff:1.1.0.0,120,1814991,35,105::ffff:1.3.0.0,112,1809858,23.1167,113.25::ffff:1.2.64.0,114,1809858,23.1167,113.25::ffff:1.2.32.0,115,1809858,23.1167,113.25::ffff:1.2.16.0,116,1809858,23.1167,113.25::ffff:1.2.0.0,119,1814991,35,105::ffff:1.2.2.0,120,1814991,35,105::ffff:1.2.6.0,119,1810821,26.0614,119.3061::ffff:1.2.4.0,120,1814991,35,105::ffff:1.2.5.0,120,1810821,26.0614,119.3061::ffff:1.2.12.0,118,1809858,23.1167,113.25[root@gameserver tmp]# cat a.sh # 脚本内容#!/bin/bashkey=`cat 1.csv | wc -l`a=(`awk -F , '{print $3}' 1.csv`)b=(`awk -F , '{print $4}' 1.csv`)for ((i=0;i<$key;i++)) { sum=$(echo ${a[$i]} \* ${a[$i]} \+ ${b[$i]} \* ${b[$i]} | bc) echo $sum >> a.txt}[root@gameserver tmp]# ./a.sh # 执行脚本[root@gameserver tmp]# cat 1.csv | wc -l # 原数据行9181[root@gameserver tmp]# cat a.txt | wc -l # 对应的值也有这么多行9181
*、批量用新文件中的数据替换旧文件中的数据
[root@localhost tmp]# cat a.txt # 要被替换的文件a b c 192.168.1.1 da c b 192.168.1.2 ea f v 192.168.1.3 f[root@localhost tmp]# cat b.txt # 用来替换的数据172.16.1.1172.16.1.2172.16.1.3[root@localhost tmp]# ./c.sh[root@localhost tmp]# cat a.txt # 替换结果a b c 172.16.1.1 da c b 172.16.1.2 ea f v 172.16.1.3 f[root@localhost tmp]# cat c.sh # 脚本内容#!/bin/basholdip=(`awk '{print $4}' ./a.txt`)newip=(`cat b.txt`)for ((i=0;i<3;i++)) { sed -i "s/${oldip[$i]}/${newip[$i]}/" ./a.txt}
*、变量赋值 (将26个小写字母赋值给一个变量)
[root@localhost tmp]# echo a={a..z} | echo $aa b c d e f g h i j k l m n o p q r s t u v w x y z
*、使用 expect 让 shell 可以自动完成登录多台服务器并执行相应的命令
#!/bin/bash####################################### ## RedHat 小怪兽 ## ## 2014-06-12 ## #######################################list=("192.168.1.81 root 123456 df1" "192.168.1.82 root 123456 df2")length=${#list[@]} ## 获取变量的个数,一个双引号为一个变量值#echo $lengthfor ((i=0;i<$length;i++)) { host=`echo ${list[$i]} | awk '{print $1}'` user=`echo ${list[$i]} | awk '{print $2}'` pass=`echo ${list[$i]} | awk '{print $3}'` desc=`echo ${list[$i]} | awk '{print $4}'` ## 将每个变量值中的位数分别赋值给新的变量名# echo $a $b $c expect << EOF spawn ssh $user@$host; expect { "(yes/no)?" { send "yes\r"; exp_continue } ## 第一次登陆会提示 yes or no "password:" { send "$pass\r" }} expect "#" send "hostname > $desc.txt\rdf -h >> $desc.txt\rexit\r" ## 多个命令可以直接连着写,记得加 \r expect eof ## 等待进程结束EOF# sleep 5}for ((i=0;i<$length;i++)) { host=`echo ${list[$i]} | awk '{print $1}'` user=`echo ${list[$i]} | awk '{print $2}'` pass=`echo ${list[$i]} | awk '{print $3}'` desc=`echo ${list[$i]} | awk '{print $4}'` expect << EOF spawn scp $user@$host:/root/$desc.txt . expect "password:" send "$pass\r" expect eofEOF}cat df*.txt > disk_sum.txt && rm -rf df*.txt ## 将下载到的不同服务器的文件放到一起,方便查看*、已知一些网卡名称,分别判断机器中的网卡名称是什么,并查看其连接状态
#/bin/bashpath=/etc/sysconfig/network-scripts/div=`ls $path | awk '{print $1}' | grep ifcfg | awk -F - '{print $2}'`for i in $divdofor k in eth0 p5p1 em1 p7p1 enp2s0doif [ $k == $i ]thenmii-tool -v $i | grep status | echo -e '\e[32m'$i'\e[m'`awk '{print $0}'`fidonedone[root@localhost tmp]# ./a.sheth0 basic status: autonegotiation complete, link ok ## 最终效果
0 0
- 【Unix/Linux.Shell Script】Shell & Shell Script
- LINUX shell script 打包
- linux shell script 入门
- linux shell script 函式
- linux shell script
- Linux shell script
- [linux]shell script 随记
- Linux Shell Script Study
- FTP linux shell script
- linux-shell script
- Linux Shell Script目录
- Linux shell script
- linux shell script 练习
- Linux-Shell script
- Linux shell script学习
- 13linux script - shell script 例子
- A shell script, in Linux.
- linux shell script 例子1
- Trie树 - TeraSort 里 自定义 partition 时乃至
- .Net MVC浅尝辄止
- 编译webrtc
- 怎样通过浏览器判断app是否已安装
- OJ_1080 进制转换
- Linux shell script
- Android利用Volley异步加载数据(JSON和图片)完整示例
- tq210 nandflash 驱动
- target runtime Apach Tomcat v6.0 is not ...
- select option标签
- Linux 内核模块中socket占用端口不能释放解决办法
- OJ_1081 递推数列
- iOS Timer--你不知道的那些事儿
- Java RMI之HelloWorld