Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Running a variable as a command.

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
Munty Scruntfundle
Posts: 54
Joined: 2018-11-27 16:53

Running a variable as a command.

#1 Post by Munty Scruntfundle »

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.


User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: Running a variable as a command.

#3 Post by debiman »

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.

xepan
Posts: 89
Joined: 2018-11-28 06:38

Re: Running a variable as a command.

#4 Post by xepan »

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/

Post Reply