Register
It is currently Mon Sep 01, 2014 3:03 pm

Assigning results of ID command to a variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Feb 13, 2010 12:51 pm   

Joined: Sat Feb 13, 2010 12:37 pm
Posts: 2
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,


Top
 Profile  
 PostPosted: Sat Feb 13, 2010 1:03 pm   

Joined: Sat Feb 13, 2010 12:37 pm
Posts: 2
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...?


Top
 Profile  
 PostPosted: Sat Feb 13, 2010 4:57 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
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?


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP