简体   繁体   English

Bash脚本在系统启动时执行命令

[英]Bash script to execute commands on system boot up

I am trying to make a bash script to check for two data values and weather or not they are set to 1 or 0 I want to do something. 我试图做一个bash脚本来检查两个数据值和天气是否将它们设置为1或0我想做点什么。 These values usually are set on system bootup and are defined usually after system is completely up (that is why I am using a while loop to keep checking until it is defined): 这些值通常是在系统启动时设置的,并且通常在系统完全启动后才定义(这就是为什么我使用while循环不断检查直到被定义)的原因:

So far I have this, but I am getting an error: 到目前为止,我有这个,但出现错误:

#!/bin/bash

shopt -s expand_aliases

alias gd126='gdget 126' # output is either 1 or 0
alias gd3='gdget 3'     # output is either 1 or 0
alias gd5='gdset 5 1'   # set this data to 1 

gd126  
gd3     


while true; do
        if [ gd126 -eq 0 ] && [ gd3 -eq 1 ]; then
                gd5; break;
        fi;
done

Here is the output when i run myScript.sh: 这是我运行myScript.sh时的输出:

[root@server tmp]# ./myScript.sh
0
0
./myScript.sh: [: gd126: integer expression expected
./myScript.sh: [: gd126: integer expression expected

Could someone please tell me why? 有人可以告诉我为什么吗? I tried changing '-eq' to '==' and it just stalls there with no output. 我尝试将'-eq'更改为'==',但它在那里停滞而没有输出。

There is no happy ending when using aliases, especially not in scripts. 使用别名时没有特别的结局,尤其是在脚本中。 Use functions instead. 使用函数代替。 In any case, use "$(somecommand)" if you want the output of a command, not not the command name itself: 在任何情况下,如果要输出命令而不是命令名称本身,请使用"$(somecommand)"

#!/bin/bash
gd126() {
    gdget 126
}

if [ "$(gd126)" -eq 0 ]
then
  echo "It's 0"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM