[bash] invoke output from previous command, as a command

Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
To clarify..

I'm trying to take the output from a command, and then execute that output as a command in and of it self.

I.e.:
Code:
commandA | <do something here to execute output as a command in the current environment>

To be more exact in what I am doing, I am running ssh-agent in my .bashrc and will be auto-adding my identities to the agent, but in order to do so I have to have the env variable SSH_AUTH_SOCK set with the location of the sock file that ssh-add will need to talk to.

Helpfully, the output of ssh-agent is similar to:
Code:
SSH_AUTH_SOCK=/tmp/ssh-DVbGwA4636/agent.4636; export SSH_AUTH_SOCK;
SSH_AGENT_PID=10084; export SSH_AGENT_PID;
echo Agent pid 10084;

So far I've got this:
Code:
#kill any existing ssh-agents so we can start our own and locate sock file
ps -ef | grep ssh-agent | awk '{print $2}' | xargs kill > /dev/null
ssh-agent | sed "s/^echo/#echo" > /tmp/sshstuff
chmod 600 /tmp/sshstuff
. /tmp/sshstuff
ssh-add
But I'd like to do this without use of the file.

I've tried using exec and command, neither seem to run in the same env as the current env - i.e. the env vars don't change from what they were before the script ran.
 
Caporegime
OP
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
ha, yeah I am a software developer by trade, and at the moment that means .net, but I do dabble lots and have used *nix since late 90s.

I probably should have warned that this is used in an msysgit installation so some of the commands are limited. I don't have pkill for a start, hence the abomination of pipes you see in my original post :)

However, I've changed it quite a lot so I don't need to actually kill any of the existing processes.

Here's what I have right now:

Code:
SSH_ENV="$HOME/.ssh/environment"
 
if [ -f "$SSH_ENV" ]; then
	. "$SSH_ENV"
fi

# test for identities
function add_identities {
	# test whether standard identities have been added to the agent already
	ssh-add -l > /dev/null 2>&1
	
	if [ $? -eq 2 ]; then
		echo "Initializing new SSH agent..."
		# spawn ssh-agent
		ssh-agent | sed "s/^echo/#echo/" > "$SSH_ENV"
		chmod 600 "$SSH_ENV"
		. "$SSH_ENV"
		echo succeeded
	fi

	ssh-add -l > /dev/null 2>&1
	if [ $? -eq 1 ]; then
		ssh-add
	fi
}
 
add_identities
Which is a butchered version taken from:
https://help.github.com/articles/working-with-ssh-key-passphrases

Unfortunately exec ssh-agent bash doesn't work, either. This is probably some kind of limitation in msysgit so I'll leave it as is. Thanks for the help.
 
Last edited:
Associate
Joined
8 Nov 2010
Posts
27
Could be worth having a look at the backtick operator(`) as it executes whatever is within the backticks.

e.g. rm `find . -name *.html`

it's possible you may be able to just do `command` which should run the command and then treat the output as the command to run.

If that doesn't work then try a bash -c "commands" and used the backticks in the command string i.e. bash -c "`command`"
[bash -c passes a string to bash to run as though it were a command line].

Good luck.
 
Back
Top Bottom