2017年7月27日星期四

Linux shell 比较运算符

please refer to http://blog.csdn.net/ithomer/article/details/6836382

运算符描述示例
文件比较运算符
-e filename如果 filename 存在,则为真[ -e /var/log/syslog ]
-d filename如果 filename 为目录,则为真[ -d /tmp/mydir ]
-f filename如果 filename 为常规文件,则为真[ -f /usr/bin/grep ]
-L filename如果 filename 为符号链接,则为真[ -L /usr/bin/grep ]
-r filename如果 filename 可读,则为真[ -r /var/log/syslog ]
-w filename如果 filename 可写,则为真[ -w /var/mytmp.txt ]
-x filename如果 filename 可执行,则为真[ -L /usr/bin/grep ]
filename1 -nt filename2如果 filename1 比 filename2 新,则为真[ /tmp/install/etc/services -nt /etc/services ]
filename1 -ot filename2如果 filename1 比 filename2 旧,则为真[ /boot/bzImage -ot arch/i386/boot/bzImage ]
字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法)
-z string如果 string 长度为零,则为真[ -z "$myvar" ]
-n string如果 string 长度非零,则为真[ -n "$myvar" ]
string1 = string2如果 string1 与 string2 相同,则为真[ "$myvar" = "one two three" ]
string1 != string2如果 string1 与 string2 不同,则为真[ "$myvar" != "one two three" ]
算术比较运算符
num1 -eq num2等于[ 3 -eq $mynum ]
num1 -ne num2不等于[ 3 -ne $mynum ]
num1 -lt num2小于[ 3 -lt $mynum ]
num1 -le num2小于或等于[ 3 -le $mynum ]
num1 -gt num2大于[ 3 -gt $mynum ]
num1 -ge num2大于或等于[ 3 -ge $mynum ]

算术运算符 
+ - * / % 表示加减乘除和取余运算
+= -= *= /= 同 C 语言中的含义

位操作符
> >>= 表示位左右移一位操作
& &= | |= 表示按位与、位或操作
~ ! 表示非操作
^ ^= 表示异或操作 

关系运算符 
= == != 表示大于、小于、大于等于、小于等于、等于、不等于操作
&& || 逻辑与、逻辑或操作

echo换行输出 echo -e "Hello world.\nHello girl!"

echo -e "Hello world.\nHello girl!"

record command history with time-stamp

#在/etc/profile中增加环境变量HISTTIMEFORMAT="%F %T "
sed -i '/export/iHISTTIMEFORMAT="%F %T "\n' /etc/profile

#export 环境变量
sed -i '/export/s/$/ HISTTIMEFORMAT/' /etc/profile


#当前shell下即刻生效
export HISTTIMEFORMAT="%F %T "

2017年7月24日星期一

Linux 设置、删除环境变量

更多请参考:http://elf8848.iteye.com/blog/1859811

××  /etc/rc.local  重点,你想让Nginx,MySQL,Tomcat自启动,请修改这个文件。

××  /etc/rc.shutdown 重点,在操作系统关机时执行,可关闭Nginx,MySQL,Tomcat

设置环境变量

如果使用的是bash,则键入如下命令:

JAVA_HOME=/ path/ to/ jdk

export JAVA_HOME

其中/path/to/jdk是安装Java的路径。


如果使用的是tcsh,则键入如下命令:

setenv JAVA_HOME /path/to/jdk



删除环境变量

bash下

设置:export 变量名=变量值

删除:unset 变量名


csh下

设置:setenv 变量名 变量值

删除:unsetenv 变量名

2017年7月21日星期五

egrep 常用命令

egrep  -o     #only print out the matched string
egrep  -v    # reverse match the lines
egrep

` 反引号
. 点号
' 单引号
" 双引号


Example: filter all the valid IP addresses in a Linux OS

#ip addr | egrep "([0-9]{1,3}.){3}[0-9]{1,3}/" | egrep -v '127.0.0|secondary' |  awk '{print "ifconfig "$7" "$2" up"}' >  ./ip-up.sh

#ip addr | egrep  '“([0-9]{1,3}.){3}[0-9]{1,3}/” | secondary'  |  awk '{print "ifconfig "$8" "$2" up"}' >>  ./ip-up.sh

#sed 's/up/down/g' ./ip-up.sh > ip-down.sh



使用egrep一次查找多个串: copy
  1. egrep "desktop|mysql|ntp" /etc/passwd"  

grep不显示本身进程
命令:
   ps aux | grep [s]sh

ps aux | grep \[s]sh
因为grep进程的条目显示处理命令优先于正则表达式([s]ome_string)。当grep命令运行时,grep进程自身的条目不匹配,就没有包括在输出中了。

ps aux | grep ssh | grep -v "grep"
输出:
[root@localhost test]# ps aux|grep ssh
root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd
root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 
root  16901  0.0  0.0  61180   764 pts/0  S+   20:31   0:00 grep ssh
[root@localhost test]# ps aux|grep \[s]sh]
[root@localhost test]# ps aux|grep \[s]sh
root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd
root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 
[root@localhost test]# ps aux | grep ssh | grep -v "grep"
root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd

root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0

Amazing Windows OS resources Share!!!

Almost all Windows OS List: https://www.heidoc.net/php/myvsdump_details.php?id=P1521F62172Ax64Lcn Latest keys:   https://bbs.kafan.cn/th...