Operating System - HP-UX
1752701 Members
6911 Online
108789 Solutions
New Discussion юеВ

Shell script:using a variable inside of a list

 
SOLVED
Go to solution
Edward McCouch
Frequent Advisor

Shell script:using a variable inside of a list

Hello all!

I am writing a simple shell script that creates directories using a "for i in $list" loop. I decided to get fancy and I want to pass a value into the list, but it isn't working. Can someone help me out? When the script gets to the variable, it doesn't read in the value, it just tries to create /fs/dir/${var} instead of /fs/dir/blah.

Here is a sample:

echo "What hostname?"
read myhost
list='fs1 fs2 fs2/dir fs2/dir/${myhost}'
for i in $list
do
echo "\tChecking /$i structure...."
if [ -e "/${i}" ]
then
echo "\t\t/${i} exists! Moving On!"
else
echo "\t\t/${i} does not exist! Creating..."
mkdir /${i}
fi
done

Operating system: HP-UX 11.i
Shell: posix

thanks is advance.
~Ed
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Shell script:using a variable inside of a list

Specify your list in double quotes rather than single quotes. Anything inside single quotes does not get expanded.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Shell script:using a variable inside of a list

Perhaps a better choice of words would be "instantiated" (to go from the abstract to the concrete) rather than "expanded" --- but you get the idea.
If it ain't broke, I can fix that.
RolandH
Honored Contributor

Re: Shell script:using a variable inside of a list

Can it be that your qouting is wrong?

Use backqoutes or in your case doubleqoutes will work, too.

list="fs1 fs2 fs2/dir fs2/dir/${myhost}"

Roland

Sometimes you lose and sometimes the others win
Edward McCouch
Frequent Advisor

Re: Shell script:using a variable inside of a list

That worked! Thank you!