Running a variable as a command.

Need help with C, C++, perl, python, etc?

Running a variable as a command.

Postby Munty Scruntfundle » 2018-11-27 20:10

Hi folks.

A quick question if I may, I'm slowly getting used to bash, but my experience with mid level languages is very limited.

If I try this;
-# ssh -t user@192.168.123.456 'sudo su; bash -l'
I get prompt in the terminal logged on as su. I'm not actually going to use this, well, maybe I will I'm not sure.

But if in a script I say;
address="user@192.168.123.456"
sudo="'sudo su; bash -l'"
sh="ssh -t"
run="$ssh $address $sudo"
echo $run
`run`

the output is;
-# ssh -t user@192.168.123.456 'sudo su; bash -l'
bash: sudo su; bash -l: command not found - And variations of command not found with exec and and everything else I've tried. However, if I copy and paste the echo output and run it as a command it runs perfectly.

So where is the command going wrong when running it, and what have I done wrong? I have an idea the output is being run as one long command line, so it needs formatting, but I really don't know.

Many thanks.
Munty Scruntfundle
 
Posts: 5
Joined: 2018-11-27 16:53

Re: Running a variable as a command.

Postby Head_on_a_Stick » 2018-11-28 06:00

dbruce wrote:Ubuntu forums try to be like a coffee shop in Seattle. Debian forums strive for the charm and ambience of a skinhead bar in Bacau. We intend to keep it that way.
User avatar
Head_on_a_Stick
 
Posts: 8322
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Running a variable as a command.

Postby debiman » 2018-11-28 06:37

Munty Scruntfundle wrote:`run`

have you just tried
Code: Select all
$run

instead?

i have done this sometimes successfully, sometimes, in cases like your's where variables are nested like that, it took some fiddling to make it work.

depending on who you ask, this is an evil thing to do and eval should be used instead, or the other way around, so take it with a grain of salt and be aware of possible security implications.

btw, what is the purpose of this?
quickly starting a root shell over ssh? is separately typing "su" too much for you?
you know it will still ask your password, right?
even so, it seems vaguely unsafe and unnecessary.
User avatar
debiman
 
Posts: 3064
Joined: 2013-03-12 07:18

Re: Running a variable as a command.

Postby xepan » 2018-11-28 06:46

The answer is easy: Don't.
Variables hold data. Functions hold code. Don't put code inside variables!

from here http://mywiki.wooledge.org/BashFAQ/050

Once at it make sure to read the common pitfalls: http://mywiki.wooledge.org/BashPitfalls

And on a last note:
08:32 <greybot> Google is NOT a preferred source for learning bash, because almost all the "tutorials" and scripts out there are JUNK. Instead,
ask a good question here or refer to the Guide and FAQ (see topic)

08:32 <greybot> Like virtually every other web site you find via Google, StackOverflow is chock full of wrong answers. Verify everything you read
there before using it.

Same is valid for forums, as the people who answer will do a quick search and post what they have found.
Use the above guide or bash-hackers instead: http://wiki.bash-hackers.org/
xepan
 
Posts: 14
Joined: 2018-11-28 06:38


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable