dave111
dave111

Reputation: 305

unix shell script syntax issue?

I have an environment variable

net_connect="@oradb"

So I am trying to store that value in another environment variable without the @ character

So doing this seems to work:

echo "test=" $net_connect | cut -d "@" -f2
test=oradb

Now if I do this I get:

SID=`$net_connect | cut -d "@" -f2`
echo "SID=" $SID
SID=

Why is my output blank?

Upvotes: 1

Views: 57

Answers (2)

John1024
John1024

Reputation: 113864

To remove the @, use prefix removal:

$ net_connect="@oradb"
$ sid="${net_connect#@}"
$ echo "$sid"
oradb

Notes:

  1. In general, ${variable#prefix} removes the glob prefix from variable. For more information, open man bash and search on Remove matching prefix pattern

  2. In shell, it is best practices to name your variables with mixed or lower case. If you use upper case variable names, then eventually you will, by accident, overwrite one of the system's variables and the results will be unpleasant.

  3. The following command does not do what you hope:

    SID=`$net_connect | cut -d "@" -f2`
    

    It runs the command $net_connect and sends its stdout to cut. But, there is likely no such command and the shell will issue an error such as:

    bash: @oradb: command not found
    

    If you really wanted to use cut, then:

    sid=$(echo "$net_connect" | cut -d "@" -f2)  # This works but prefix removal is simpler
    

Upvotes: 4

Marc B
Marc B

Reputation: 360702

Because you're executing the variable, not echoing it:

$net_connect | etc...

boils down to

@oradb | etc...

which is a syntax error. You have no command/binary named @oradb.

Maybe you wanted

SID=`echo $net_connect | cut -d "@" -f2`

instead.

Upvotes: 2

Related Questions