LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2014, 08:00 PM   #1
hoisu
LQ Newbie
 
Registered: Apr 2014
Location: Cluj-Napoca
Distribution: Ubuntu
Posts: 6

Rep: Reputation: Disabled
Unhappy bash won't search within a variable with a user imputed variable


Hi Everybody ,

I hope i don't mess it up because this is my first post on a forum ,so please excuse me if I'm doing something wrong , so let's get into it .

So i have this bash script , in it I have these 2 variables :
ex :

a=($ls)
b=$1 - this will be a user input (example read -p "enter a variable: " b )

My question is : How can i search the for $b in $a (if i want to find for example the file named a )

I have already tried these options and didn't work :

if [[ ! "$b" =~ $a]] ; then

echo "The variable '${b}' does not exist "


exit 1
fi


and i tried whit grep like this :
grep -w -h ${b} ${a}

but no luck , if anyone can give me an idea how to go forward with this ,I would be very thankful .

thanks guys ,

Last edited by hoisu; 04-01-2014 at 08:01 PM.
 
Old 04-01-2014, 11:22 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
please insert your complete script between [code] and [/code] tags
please explain this line:
Code:
a=($ls)
please use this command in your script:
Code:
set -xv
 
1 members found this post helpful.
Old 04-02-2014, 02:37 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Yes it does feel like you have given only part of the script and that you may well have some typos, ie you didn't copy and paste but tried to type it by hand.

Based on what I sort of see, I would hazard a guess at the following:

1. 'a' is supposed to be an array comprised of the output of ls, if correct, the assignment should have been:
Code:
a=($(ls))
As a side note, if any of your files or directories have spaces in them, this will cause you all sort of headaches

2. You have given us 2 different options on how 'b' has been assigned:
Code:
# either the user passes the value to the script
b=$1

# or the user is requested for the information within the script and hence 'b' is assigned using read command
read -p "Enter a value for 'b': " b
Either can be used but not both as one will overwrite the other

Assuming all of the above is correct, the easiest solution is to use a for loop to iterate over the array and test each item against the value of 'b'

If I am way off, please explain further
 
1 members found this post helpful.
Old 04-02-2014, 06:11 AM   #4
hoisu
LQ Newbie
 
Registered: Apr 2014
Location: Cluj-Napoca
Distribution: Ubuntu
Posts: 6

Original Poster
Rep: Reputation: Disabled
Smile It Works :)

So this is the entire code :

Code:
echo "You have the following drives available : "

#I'm using curently cygdrive to check which disks i have mounted 


cd /cygdrive 
ls 


#reading the user input 

read -p "Enter the drive letter : " disk

#checking if drive letter was introduced 

if [[ ! $disk ]] ; then 

echo "You didn't introduce a drive letter   " 
 
exit 1 

fi 


#check if drive letter exists (with the ls command)

cd /cygdrive 
servers=($(ls))
#the problem was here , servers was declared as servers=$(ls) , but now i declared it as servers=($(ls)) and it works fine

if  [[ ! "${disk}" =~ ${servers}  ]]  ; then  

echo "Disk  '${disk}'  does not exist  " 


exit 1 
fi



echo "end of code " 

exit 0
@ grail thank you very much , I've found the problem , it seems that you were right !

Thanks everyone for the help!
 
Old 04-02-2014, 07:28 AM   #5
sashi_hc
LQ Newbie
 
Registered: Aug 2012
Posts: 6
Blog Entries: 1

Rep: Reputation: Disabled
You are basically trying to see if a variable is set

Why not simply use the -z option? This is one of the moost efficient way I can think of.

if [ -z $myvariable ]
then

fi
 
Old 04-02-2014, 07:39 AM   #6
hoisu
LQ Newbie
 
Registered: Apr 2014
Location: Cluj-Napoca
Distribution: Ubuntu
Posts: 6

Original Poster
Rep: Reputation: Disabled
-z

Quote:
Originally Posted by sashi_hc View Post
Why not simply use the -z option? This is one of the moost efficient way I can think of.

if [ -z $myvariable ]
then

fi

Because i needed to find a variable with another variable .
 
Old 04-02-2014, 08:50 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
it is sg like this:

Code:
if [ -d /cygdrive/"$disk" ]; then echo 'Found';
else echo 'Not found';
fi
 
Old 04-02-2014, 09:23 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
ummmm .... how does disk contain servers?? Shouldn't the servers, ie the array with multiple items in it, contain the disk?

Also, can you guarantee that all disks are unique and hence using a none enclosed regex will not potentially match other items?

I am going to go out on a limb here and say that if you explained what you actually need to do that someone may have a much better method as the current one seems terribly flawed.
 
1 members found this post helpful.
Old 04-02-2014, 12:11 PM   #9
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Quote:
Originally Posted by hoisu View Post
and i tried whit grep like this :
grep -w -h ${b} ${a}

but no luck
The syntax would be
Code:
echo "$a" | grep -w "$b"
That is, $a contains the content to be searched, not the name of a file to be searched. You can make grep work on that content by piping it to grep's stdin, which grep reads by default when no file is specified.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash search for a pattern within a string variable nutthick Programming 8 03-17-2015 07:26 PM
[SOLVED] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 10 02-15-2015 04:43 PM
[SOLVED] Bash script question, searching in a variable, displaying search results... Wolfjurgen Programming 5 04-26-2011 12:29 PM
Bash: How do I read data from a variable while that variable is being populated? theaceoffire Programming 4 04-23-2010 02:29 PM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration