0

I am trying to use variables to construct the --command arg for gnome-terminal.

My shell script look like this:

buildId="aa-bb-cc"
versionCode="123456"
daily="daily"

gnome-terminal -e 'sh -c "while true; do
        python acra.py $versionCode $buildId 0 $daily
        sleep 600 # 10 mins
    done"'

But when I run this script, new terminal open but it cannot recognize these variables, I'm only receive sys.argv = ['acra.py', '0'] inside my python script, so I guess the cmd was executed just like:

python acra.py 0

So how can I use variable in this case?

2

Assuming that gnome-terminal behaves like xterm:

gnome-terminal -e sh -c 'some commands here' sh "$variable1" "$variable2" "etc."

The strings at the end of the command line will be available inside the sh -c script as $1, $2, $3, etc. The first argument to the script, the string sh, will be placed in $0 and used in error messages by the shell.

In your case:

#!/bin/sh

buildId="aa-bb-cc"
versionCode="123456"
daily="daily"

gnome-terminal -e sh -c '
    while true; do
        python acra.py "$1" "$2" 0 "$3"
        sleep 600
    done' sh "$versionCode" "$buildId" "$daily"

This assumes that the acra.py script is available in the current working directory.

|improve this answer|||||
2

Kusalananda answer gives an error.

Using the -x option instead of the -e option, it will be fix error.

I used echo instead of python in the following examples for easy error detection.

buildId="aa-bb-cc"
versionCode="123456"
daily="daily"

gnome-terminal -e sh -c \
  'echo "$1" "$2" 0 "$3"; sleep 60' \
  sh "$versionCode" "$buildId" "$daily"

result is: Failed to parse arguments: Unknown option -c

buildId="aa-bb-cc"
versionCode="123456"
daily="daily"

gnome-terminal -x sh -c \
  'echo "$1" "$2" 0 "$3"; sleep 60' \
  sh "$versionCode" "$buildId" "$daily"

Opens new terminal window, and print this result in new window : 123456 aa-bb-cc 0 daily

From gnome-terminal man page:

-e, --command=STRING

Execute the argument to this option inside the terminal.

-x, --execute

Execute the remainder of the command line inside the terminal.

The correct answer with Kusalananda approach :

#!/bin/sh

buildId="aa-bb-cc"
versionCode="123456"
daily="daily"

gnome-terminal -x sh -c '
    while true; do
        python acra.py "$1" "$2" 0 "$3"
        sleep 600
    done' sh "$versionCode" "$buildId" "$daily"

Answer according to your (nhoxbypass) example :

buildId="aa-bb-cc"
versionCode="123456"
daily="daily"

gnome-terminal -e "sh -c \"while true; do
        python acra.py $versionCode $buildId 0 $daily
        sleep 600 # 10 mins
    done\""

or

buildId="aa-bb-cc"
versionCode="123456"
daily="daily"

gnome-terminal -x sh -c "while true; do
        python acra.py $versionCode $buildId 0 $daily
        sleep 600 # 10 mins
    done"
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.