从零开始学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
[root@localhost learn_shell]# echo -e "1\t2\t3"
1 2 3
cat /proc/$PID/environ
[root@localhost learn_shell]# pgrep bash #pgrep 获取bash的进程ID
18585
cat /proc/18585/environ |tr '\o' '\n' # \o代表null字符,tr将\o替换为\n
[root@localhost learn_shell]# var='value'
[root@localhost learn_shell]# echo $var
value
[root@localhost learn_shell]# echo ${var}
value
[root@localhost learn_shell]# cat variables.sh
#!/bin/bash
#file_name:variables.sh
fruit=apple
count=5
echo "We have $count ${fruit}(s)"
[root@localhost learn_shell]# sh variables.sh
We have 5 apple(s)
[root@localhost 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
[root@localhost learn_shell]# var=123456
[root@localhost 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
[root@localhost learn_shell]# sh chkuser.sh
Root USER
[root@localhost learn_shell]# cat test_let.sh
#!/bin/bash
#file_name:test_let.sh
no1=4;
no2=5;
let result=no1+no2
echo $result
[root@localhost 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
[root@localhost learn_shell]# no1=54
[root@localhost learn_shell]# result=`echo "$no1 * 1.5" | bc`
[root@localhost learn_shell]# echo $result
81.0
[root@localhost learn_shell]# echo "scale=2;3/8" | bc
.37
[root@localhost learn_shell]# echo "scale=3;3/8" | bc
.375
[root@localhost learn_shell]# echo "sqrt(100)" | bc
10
[root@localhost learn_shell]# echo "10^10" | bc
10000000000
[root@localhost learn_shell]# echo "10^2" | bc
100
posted on 2019-02-14 13:29 William126 阅读(178) 评论(0) 编辑 收藏 举报