Skip to content
Advertisement

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. 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):

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:

[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.

Advertisement

Answer

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:

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

if [ "$(gd126)" -eq 0 ]
then
  echo "It's 0"
fi
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement