一个shell脚本bug

sh/bash/dash/ksh/zsh等Shell脚本
回复
chu887
帖子: 116
注册时间: 2008-11-06 22:09

一个shell脚本bug

#1

帖子 chu887 » 2017-01-25 13:26

我在.bash_profile中添加一个登录后执行命令, 如下:

代码: 全选

if [ "tty | grep tty1" ];	then 
	startx 
fi
即tty的返回结果中出现tty1 才执行 startx命令;其他的tty则不进入xwindow
可实际测试 时 登录tty2后 也会执行 startx , 但我没发现错在哪儿, 还请大家帮忙看看。
PS:好久不写shell脚本,再回头真觉得晦涩。
头像
lilydjwg
论坛版主
帖子: 4249
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 一个shell脚本bug

#2

帖子 lilydjwg » 2017-01-25 15:09

代码: 全选

if [[ $(tty) == *tty1* ]]; then
bash / zsh.
chu887
帖子: 116
注册时间: 2008-11-06 22:09

Re: 一个shell脚本bug

#3

帖子 chu887 » 2017-01-25 16:10

谢谢楼上,你的是可以的。我发现我错在哪儿了,应该是

代码: 全选

if [ `tty|grep tty1` ] ;then 
之前 双引号之间的内容 会被当作字符串,自然不为空, 总能执行then后面的语句。
回复