从零开始学Shell(一)

1.$表示普通用户,#表示超级用户。
2.sh /home/path/script.sh
3.chmod a+x script.sh  增加可执行权限
    ./script.sh    #./表示当前的目录
4.终端打印:echo 'I love linux'
            
#!/bin/bash
#file_name:printf.sh

printf "%-5s %-10s %-4s\n" No Name Mark
printf "%-5s %-10s %-4.2s\n" 1 Sarath 80.3456
printf "%-5s %-10s %-4s\n" 2 James 90.9989
printf "%-5s %-10s %-4s\n" 3 Jeff 77.546

%s,%c,%d,%f都是格式替代符,%-5s指明了一个格式为左对齐且宽度为5的字符串替代,没有-表示右对齐;


sh printf.sh
chmod a+x printf.sh
./printf.sh

[[email protected] learn_shell]# echo -e "1\t2\t3"
1    2    3

cat /proc/$PID/environ
[[email protected] learn_shell]# pgrep bash  #pgrep 获取bash的进程ID
18585

cat /proc/18585/environ |tr '\o' '\n' # \o代表null字符,tr将\o替换为\n

[[email protected] learn_shell]# var='value'
[[email protected] learn_shell]# echo $var
value
[[email protected] learn_shell]# echo ${var}
value


[[email protected] learn_shell]# cat variables.sh
#!/bin/bash
#file_name:variables.sh
fruit=apple
count=5
echo "We have $count ${fruit}(s)"
[[email protected] learn_shell]# sh variables.sh
We have 5 apple(s)

[[email protected] learn_shell]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin

$PATH通常定义在以下三个文件中
cat /etc/environment
cat /etc/profile
cat ~/.bashrc

给$PATH添加一条新的路径
export PATH="$PATH:/home/user/bin"

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

echo $HOME
echo $PWD
echo $USER
echo $UID
echo $SHELL    =    echo $0

[[email protected] learn_shell]# var=123456
[[email protected] learn_shell]# echo ${#var}
6

#!/bin/bash
#file_name:chkuser.sh

if [ $UID -ne 0 ];then
echo None root user.Please run as root
else
echo "Root USER"
fi
[[email protected] learn_shell]# sh chkuser.sh
Root USER


[[email protected] learn_shell]# cat test_let.sh
#!/bin/bash
#file_name:test_let.sh
no1=4;
no2=5;

let result=no1+no2
echo $result
[[email protected] learn_shell]# sh test_let.sh
9

let no1++ 自加操作
let no1-- 自减操作

let no1+=6   (no=no+6)
let no2-=6   (no=no-6)

result=$[ no1 + no2 ]
result=$[ $no1 + 5 ]
result=$(( no1 + 50 ))

result=`expr 4 + 4`
result=$(expr $no1 + 5)

echo "4 * 0.56" | bc
[[email protected] learn_shell]# no1=54
[[email protected] learn_shell]# result=`echo "$no1 * 1.5" | bc`
[[email protected] learn_shell]# echo $result
81.0

[[email protected] learn_shell]# echo "scale=2;3/8" | bc
.37
[[email protected] learn_shell]# echo "scale=3;3/8" | bc
.375

[[email protected] learn_shell]# echo "sqrt(100)" | bc
10
[[email protected] learn_shell]# echo "10^10" | bc
10000000000
[[email protected] learn_shell]# echo "10^2" | bc
100







猜你喜欢

转载自www.cnblogs.com/william126/p/10374093.html
0条评论
添加一条新回复