[SOLVED] bash won't search within a variable with a user imputed variable
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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'
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 !
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.