SHELL 学习

Date

$ date
2014年11月19日 星期三 21时16分17秒 CST
$ date +%s
1416402983
$ date "+%d %B %Y"
19 十一月 2014
$ start=$(date +%s)
$ echo $start
1416403040
$ end=$(date +%s)
$ echo $end
1416403066
$ diff=$((end-start))
$ echo $diff
26



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 5
echo ${arry[@]}
1 2 3 4 5
echo ${arry[2]}
3
index=3
echo ${arry[$index]}
4
#print length of array
echo ${#arry[*]}
5
arry_var[0]="danio"
arry_var[1]="mia"
arry_var[3]="bing"
echo ${arry_var[*]}
danio mia bing
echo ${arry_var[1]}
mia
echo ${#arry_var[*]}
3

输入输出重定向

0 - stdin

1 - stdout
2- stderr

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


ls ! 2>err.txt 1>out.txt
cat err.txt
ls: !: No such file or directory
cat out.txt
echo $?
0
ls !
ls: !: No such file or directory
echo $?
1

#covert stderr to stdout and redirect to log.txt file by using 2>&1 or &>
ls ! 2>&1 log.txt
ls: !: No such file or directory
log.txt
cat log.txt
ls: !: No such file or directory
ls ! &>log2.txt
cat log2.txt
ls: !: No such file or directory
</pre><pre name="code" class="python"><pre name="code" class="python">#cleanup stderr
ls ! 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 overwrite
ls | tee -a out.txt | cat -n


 

bc 数学高级运算

echo "4 * 5.5" | bc
#output 22.0

no=1000
echo "obase=2;$no" | bc
#output 1111101000

echo "scale=3; 3/7" | bc
#output .428 (keep 3 points)

echo "sqrt(10000)" | bc
echo "100^2" | bc
#output:
#100
#10000

let, [], 整形运算

num1=2;
num2=4;
let result=num1+num2
echo $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=100
let result=var*10
echo $result
1000
result1=$[var*100]
echo $result1
10000


 

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

var1=abc
var2=def
var3=${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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值