首页  

linux shell 一些知识点     所属分类 shell 浏览量 1208
标准输入和命令参数的区别 
后台运行 和 nohup
单引号和双引号表示字符串的区别
为何有的命令和sudo一起用 command not found 

管道 |  重定向 > <   变量$

echo 'hello' | cat
ls | rm    ( 错误用法)
rm  只接收 参数

rm $(ls)
$(cmd) 读取cmd命令输出作为参数


加上&,让 shell 进程不再阻塞,可以继续响应新命令 , 退出shell之后 ,依附于他的子进程都会退出

showtime.sh 
#!/bin/bash
while(true)
do
    echo $(date) 
    sleep 1
done

添加执行权限
chmod +x showtime.sh 

./showtime.sh  & 

pstree显示进程树

ps -ps

-p, --show-pids     show PIDs; implies -c
-s, --show-parents  show parents of the selected process
  

├─sshd(16168)─┬─sshd(3534)───bash(3560)───showtime.sh(4200)───sleep(+
           │  └─sshd(4548)───bash(4576)───pstree(4928)


ps aux|grep showtime
root      4200  0.0  0.0 113180  1428 pts/0    S    13:53   0:00 /bin/bash ./showtime.sh

ps aux|grep bash
root      3560  0.0  0.1 117020  3652 pts/0    Ss+  13:51   0:00 -bash
root      4200  0.0  0.0 113180  1432 pts/0    S    13:53   0:00 /bin/bash ./showtime.sh


使用 nohup 运行

nohup ./showtime.sh  & 
输出重定向到  nohup.out

终端退出后 ,会挂到 System 进程下面

├─showtime.sh(6649)───sleep(10767)
├─sshd(16168)─┬─sshd(3534)───bash(3560)───showtime.sh(4200)───sleep(10763)

nohup cmd &  和  (cmd &)   有一样的效果
ps aux|grep showtime

(./showtime.sh > showtime.log &)  
进程直接挂在 system 下,   nohup 只有在终端退出才挂到 system 下



使用set -x命令,开启 shell 命令回显,通过回显观察 shell 到底在执行什么命令

who
root     pts/2        2020-03-01 14:17 (27.26.26.58)

echo $(who)
root pts/2 2020-03-01 14:17 (27.26.26.58)
echo "$(who)"
root     pts/2        2020-03-01 14:17 (27.26.26.58)

echo $(cmd) 和 echo "$(cmd)" 的区别

双引号转义完成的结果会自动增加单引号


sudo 找不到命令

普通用户可以用的命令,用sudo 后却报错 command not found

使用sudo时,系统认为是 root 用户在执行命令,所以会去搜索 root 用户的环境变量

使用脚本全路径

上一篇     下一篇
商业银行业务体系

世界三大泡沫事件

芯片 半导体 集成电路

疫情之后新基建

JVM编译器

线程安全 与 原子性 可见性 有序性