SHELL 学习

来源:互联网 发布:社保报盘软件下载 编辑:程序博客网 时间:2024/06/06 14:25

Date

$ date2014年11月19日 星期三 21时16分17秒 CST$ date +%s1416402983$ date "+%d %B %Y"19 十一月 2014$ start=$(date +%s)$ echo $start1416403040$ end=$(date +%s)$ echo $end1416403066$ diff=$((end-start))$ echo $diff26



Alias

usage: alias new_command='command sequence'
delete:  unalias or alias example=
note: only available for current session
    overwrite if alias is already existing
   

Array (available from bash 4.0)

arry=(1 2 3 4 5)echo $arry[*]1[*]echo ${arry[*]}1 2 3 4 5echo ${arry[@]}1 2 3 4 5echo ${arry[2]}3index=3echo ${arry[$index]}4#print length of arrayecho ${#arry[*]}5arry_var[0]="danio"arry_var[1]="mia"arry_var[3]="bing"echo ${arry_var[*]}danio mia bingecho ${arry_var[1]}miaecho ${#arry_var[*]}3

输入输出重定向

0 - stdin

1 - stdout
2- stderr

$?查看命令执行是否成功 0 - 成功, 非0 - 失败


ls ! 2>err.txt 1>out.txtcat err.txtls: !: No such file or directorycat out.txtecho $?0ls !ls: !: No such file or directoryecho $?1

#covert stderr to stdout and redirect to log.txt file by using 2>&1 or &>ls ! 2>&1 log.txtls: !: No such file or directorylog.txtcat log.txtls: !: No such file or directoryls ! &>log2.txtcat log2.txtls: !: No such file or directory
</pre><pre name="code" class="python"><pre name="code" class="python">#cleanup stderrls ! 2>/dev/null

#tee accept stdout only and will pass them to next command# following command display current folders and files, and save to out.txt, then cat -n add line number for each input from stdout.ls | tee out.txt | cat -n# use -a if tee append to file but not overwritels | tee -a out.txt | cat -n


bc 数学高级运算

echo "4 * 5.5" | bc#output 22.0no=1000echo "obase=2;$no" | bc#output 1111101000echo "scale=3; 3/7" | bc#output .428 (keep 3 points)echo "sqrt(10000)" | bcecho "100^2" | bc#output:#100#10000

let, [], 整形运算

num1=2;num2=4;let result=num1+num2echo $result#output 6
let num1++ 
echo $num1
#output 3

num1=2;num2=4;result=$[num1+num2]echo $result#output 6
</pre><pre name="code" class="javascript"><pre name="code" class="javascript">var=100let result=var*10echo $result1000result1=$[var*100]echo $result110000


$[ var1:+var2]如果var1不为空则使用vare.g. 

var1=abcvar2=defvar3=${var1:+$var1$var2}echo $var3

output is "abcdef"

export PATH="$PATH:/home/user/bin"

将/home/user/bin加入PATH环境变量中,当前的terminal session有效


echo $SHELL
/bin/bash
识别当前使用的 shell

$UID 是否为0来判断是否为root用户 

if [ $UID -ne 0 ]; then echo "non root user"else echo "root user"fi


0 0
原创粉丝点击