加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

从零开始学Shell(一)

发布时间:2020-12-15 18:21:31 所属栏目:安全 来源:网络整理
导读: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 %-4sn

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 %-4sn" No Name Mark
printf "%-5s %-10s %-4.2sn" 1 Sarath 80.3456
printf "%-5s %-10s %-4sn" 2 James 90.9989
printf "%-5s %-10s %-4sn" 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 "1t2t3"
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" | bc100

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读