BashScripts.org
http://bashscripts.org/forum/

Assigning results of ID command to a variable
http://bashscripts.org/forum/viewtopic.php?f=15&t=993
Page 1 of 1

Author:  rmmiles [ Sat Feb 13, 2010 12:51 pm ]
Post subject:  Assigning results of ID command to a variable

This is my first real bash script ( in OS X ).

Here's my question; I'm trying to put the results of the "id" command into a variable called "var1".

If I manually enter the following from terminal ( in OS X ), I get the results I expect, the "id" command is run and assigned to the variable "var1". see below:

Code:
   var1=$(echo /usr/bin/id username)
   $var1


However, If I put the same exact lines into a script and run, the variable is not assigned.

Any ideas? Many thanks,

Author:  rmmiles [ Sat Feb 13, 2010 1:03 pm ]
Post subject:  Re: Assigning results of ID command to a variable

ok, well, that was stupid of me, I just had to remove the echo and it assigned the variable.

I guess the real issue is that I am trying to check the resulting variable for a substring;

Code:
if [[ $var1 =~ "substring" ]]; then

     echo "found the substring"

else

     echo "didn't find the substring"

fi


This code doesn't seem to work, even though on all accounts from what I've read on numerous forums, it should...?

Author:  Watael [ Sat Feb 13, 2010 4:57 pm ]
Post subject:  Re: Assigning results of ID command to a variable

Hi,

it should:
Code:
$ cat aScript
#!/bin/bash
echo "$SHELL is $BASH_VERSION" #*
var1=$(/usr/bin/id $USER)
pattern="audio"
if [[ "$var1" =~ "$pattern" ]]
# or: [[ "$var1" == *"$pattern"* ]]
then echo "found the substring"
else echo "didn't find the substring"
fi
$ ./aScript
/bin/bash is 4.0.35(1)-release
found the substring
works fine for me, as I'm in audio group.

#* Are you sure you're using bash?

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com