Shell:
pkill -u myname 杀掉自己的进程
sed -i '1d' file 删掉原文第一行
sed中使用变量
此时需要把单引号改成双引号,如下边例子
$port=8001
sed -in-place "s/8080/$port/g" /home/work/server.xml
${var}等同于$var 但是前者有很多拓展,例如${#var}就是var的size
while read line/word
do
$line/word
done < file
如果脚本里调用别的地方的脚本,例如1号脚本里有这么一行:
sh /context/mywork/test.sh
那么这个2号脚本test.sh里面的脚本路径默认应该是1号脚本的位置。例如2号脚本test.sh里面有这么一行:
sh run.sh
那么这个run.sh的路径默认是在1号脚本的路径上。所以一定要注意写相对路径的时候默认的是当前目录下的路径。所以尽可能的写绝对路径吧
crontab -l 显示所有的crontab
crontab -r删除所有这个用户的crontab
格式:* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
crontab不会执行系统的自带环境,所以如果要执行Java请加上环境变量
export JAVA_HOME=/path
shell的符号运算都要在前面加$[]因为shell默认的都是字符串加减,所以传统运算需要加$[]
数组的声明:
- 1)array[key]=value # array[0]=one,array[1]=two
- 2)declare -a array # array被当作数组名
- 3)array=( value1 value2 value3 ... )
- 4)array=( [1]=one [2]=two [3]=three ... )
- 5)array="one two three" # echo ${array[0|@|*]},把array变量当作数组来处理,但数组元素只有字符串本身
- 1)${array[key]}
- # 举例 ${array[1]}
读取文件中的内容,并将其存入到变量中
var=$(cat name.txt)
用wc命令怎么做到只打印统计数字不打印文件名
命令:
输出:
[root@localhost test]# wc -l test.txt
7 test.txt
[root@localhost test]# cat test.txt |wc -l
7
函数的$1:
函数里的$1跟外面的$1不是同一个参数,所以如果要使用外面的$1要把外面的$1作为参数传到函数里
curl超时设置:
使用curl时,有两个超时时间:一个是连接超时时间,另一个是数据传输的最大允许时间。
连接超时时间用 –connect-timeout 参数来指定,数据传输的最大允许时间用 -m 参数来指定。
例如:
curl –connect-timeout 10 -m 20 “http://outofmemory.cn/”
连接超时的话,出错提示形如:
curl: (28) connect() timed out!
数据传输的最大允许时间超时的话,出错提示形如:
curl: (28) Operation timed out after 2000 milliseconds with 0 bytes received
网页返回码:
curl --write-out "%{http_code}\n" --silent --output /dev/null "$URL"
shell的大于小于等于
-ge 大于等于 -gt 大于 -eq 相等 -lt 小于 -ne 不相等
linux的mv命令技巧:
mv的效率大大高于cp,所以如果能用mv就尽量别用cp了。
如果除了某几个文件不要mv可以先把全都mv然后再mv回来那几个文件,不要一个一个mv!!!
core文件debug的时候把core mv到一个有源码的其他目录然后gdb就是了
如果要在if里面使用正则表达式可以用[[]]和=~结合
if [ $a = $b ] 是条件校验,if [[ $a == $b ]] 是模式匹配,也就是只要后面的有一部分在前面即可。然后
if [[ $a =~ $b ]] 就是正则表达式了。
Pyhon:
str.replace(old,new,max) old被new覆盖,max如果有值,那么只有前max个会被替换。
正则表达式
re.complie地址:
http://www.jb51.net/article/15707.htm
list的index
a_list.index('a',0,3) //返回a在指定切片(0到3)内第一次出现的位置
对于一般性数据量超大的list,快速清空释放内存,可直接用 a = [] 来释放。其中a为list。
对于作为函数参数的list,用上面的方法是不行的,因为函数执行完后,list长度是不变的,但是可以这样在函数中释放一个参数list所占内存: del a[:],速度很快,也彻底:)
awk:
awk '{print " ' "$LOGNAME" ' "}' yourfile
如果想调用environment variable, 要用以上的方式调用, 方法是:
" ' " $LOGNAME " ' "
以上这个方法只是调用全局变量,但是要怎么修改全局变量呢
在awk中根本就不能修改全局变量,因为在awk执行的时候,实际上是复制了一份parent进程的全局变量。所以如果awk要和父进程交换信息的话,有一个不错的办法,就是通过命令置换,把命令的输入都放到变量里去,放心,shell会自动的弱化里面的换行。
所以要通过awk修改变量的方法只有一个,例子:
i=$('ps | awk '$4 ~ /\<ed/'{print $1}`)
把变量提到前面然后用awk去print
awk 'BEGIN{system("echo abc")}' file
awk中位操作
没有按位与的操作符,只能用函数
Bit Manipulations Functions
Starting with version 3.1 of gawk, the following bit manipulation func-
tions are available. They work by converting double-precision floating
point values to unsigned long integers, doing the operation, and then
converting the result back to floating point. The functions are:
and(v1, v2) Return the bitwise AND of the values provided by v1
and v2.
compl(val) Return the bitwise complement of val.
lshift(val, count) Return the value of val, shifted left by count
bits.
or(v1, v2) Return the bitwise OR of the values provided by v1
and v2.
rshift(val, count) Return the value of val, shifted right by count
bits.
xor(v1, v2) Return the bitwise XOR of the values provided by v1
and v2.