月度归档:2023年06月

写脚本常用到的一些命令

 判断上句是否成功执行 
 [ $? -eq 0 ]    若成功则为0  若不成功 则返回值不为0

xargs传递参数命令
比如查找某名称的文件并删除他们 
find -name samba* | xargs rm 
全部干掉。

将程序输出值赋给变量
IP=$(cat index.html | grep “remoteIP =” | awk ‘{print $4}’ | cut -d “‘” -f2)
1、=号左右两边不要空格
2、如果采用$()的形式就不用反引号,如果采用“的话就不用$()

变量字符串比较
if [ “$IP” == “$SS” ]

uci获取参数指令
root@openwrt:/mnt# uci get wireless.@wifi-device[0].hwmode

11g
此时的配置文件是/etc/config/wireless
config wifi-device ‘radio0’
 option type ‘mac80211′
 option channel ’11’
 option hwmode ’11g’
 option path ‘platform/ar933x_wmac’
 option disabled ‘0’
 option htmode ‘HT40′
 option txpower ’18’
 option country ‘US’
对比颜色相同的参数,就可以理解uci读取配置文件的语法规则。

命令行下面扫描wifi指令
iwlist wlan0 scan
iw wlan0 scan

命令行下面扫描wifi并获取SSID指令
 iw wlan0 scan | grep SSID | awk ‘{print $2}’
也可以使用iwlist指令和cut分割提取方法实现
iwlist  wlan0 scan | grep SSID  | cut -d ‘”‘ -f2  注意:cut -d 后面可以使用双引号也可以使用单引号,由于此处分割的字符是双引号,所以两边只能采用单引号,这样运行中才不会出现歧义。

处理两个文件中的相同行和不同行
1. 如何取出两个文件的并集(重复的行只保留一份)?
2. 如何取出两个文件的交集(只留下同时存在于两个文件中的文件)?
3. 如何删除交集,留下其他的行?
1. cat file1 file2 | sort | uniq
2. cat file1 file2 | sort | uniq -d
3. cat file1 file2 | sort | uniq -u

linux统计文本文件中行数

wc -l 1.txt | awk ‘{print $1}’ 

Shell脚本对比两个文本文件找出不同行的方法
grep命令法
命令如下:grep -vwf file1 file2
统计file1中没有,file2中有的行

curl使用笔记
采用get方式时候,比如http://forum.rippletek.com/member.php?mod=logging&action=login
fiddler里面显示是GET /member.php?mod=logging&action=login HTTP/1.1
使用curl提交get就是 curl -G -d “mod=logging&action=login” http://forum.rippletek.com/member.php
把?去掉即可。这个问号貌似是个分隔符,没有实质意义

grep -E ‘123|abc’ filename  // 找出文件(filename)中包含123或者包含abc的行

不删除的情况下清空某个日志文件
true > 111.log
cp /etc/null 111.log

sed在每行的头添加字符,比如”HEAD”,命令如下:

sed ‘s/^/HEAD&/g’ test.file

sed在每行的行尾添加字符,比如“TAIL”,命令如下:

sed ‘s/$/&TAIL/g’ test.file

shell中变量自增的实现方法

Linux Shell中写循环时,常常要用到变量的自增,现在总结一下整型变量自增的方法。
我所知道的,bash中,目前有五种方法:
1. i=`expr $i + 1`;
2. let i+=1;
3. ((i++));
4. i=$[$i+1];
5. i=$(( $i + 1 ))
可以实践一下,简单的实例如下:
#!/bin/bash
i=0;
while [ $i -lt 4 ];
do
   echo $i;
   i=`expr $i + 1`;
   # let i+=1;
   # ((i++));
   # i=$[$i+1];
   # i=$(( $i + 1 ))
done

另外,对于固定次数的循环,可以通过seq命令来实现,就不需要变量的自增了;实例如下:
#!/bin/bash
for j in $(seq 1 5)
do
  echo $j
done

shell判断或的关系 两个条件满足一个就为真 
 if [ $price -gt $pricelimit ] || [ $price -le $lowprice ]; then
||在这里相当于或的关系 两个比较条件需要用[]分别括起来,另外[与字符串$price之间需要空格