Geodesic
Geodesic

Reputation: 893

Bash redirection help

I'm having a little issue getting quite a simple bash script running.

The part that's working:

qstat -f $queuenum | grep -Po '(?<=PBS_O_WORKDIR=).*(?=,)' 

Outputs a directory to the screen (for example):

/short/h72/SiO2/defected/2-999/3-forces/FORCES_364

All I want to do is change directory to this folder. Appending a "| cd" to the end of the above command doesn't work, and I can't quite figure out how to use the $(()) tags either.

Any help would be appreciated.

Upvotes: 0

Views: 127

Answers (3)

codaddict
codaddict

Reputation: 455020

You should use:

cd "$(qstat -f $queuenum | grep -Po '(?<=PBS_O_WORKDIR=).*(?=,)' )"

you are trying to achieve command substitution which is achieved by using either of these two syntax:

$(command)

or like this using backticks:

`command` 

The first one is the preferred way as it allows nesting of command substitutions something like:

foo=$(command1 | command2 $(command3))

also you should enclose the entire command substitution in double quotes to protect you if the result of the command substitution is a string with spaces.

Upvotes: 0

Sanjay Manohar
Sanjay Manohar

Reputation: 7026

Invoking your script creates a new bash shell

This shell is destroyed when your script ends.

If you use exec <scriptname> to run your script, the new bash shell is substituted for the current one. So if you add the command bash at the end of your script, you will get what you desire, but not the same bash shell.

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272487

cd `qstat -f $queuenum | grep -Po '(?<=PBS_O_WORKDIR=).*(?=,)' `

Upvotes: 2

Related Questions