Operating System - HP-UX
1847100 Members
5275 Online
110263 Solutions
New Discussion

Re: bash scripting tricks

 
SOLVED
Go to solution
Jason W. Neiss
Valued Contributor

bash scripting tricks

Hi, all;

I recently discovered that in bash, you can create a statement like:

bash# mkdir /mydir/{0,1,2,3}

in order to save typing.

I have a need to create and mount a large number of directories for testing purposes. So, I'd like to do something like:

bash# mkdir /mydir/{0,1,2,3}
bash# mount server:/vol/vol1/{0,1,2,3} /mydir/{0,1,2,3}

The directories I need to mount have numbers for names: 00 through 49. Questions:

1) Will that trick work with mount the same way it does with mkdir? I suspect it will, since it's a function of the shell.

2) Will it work using 00, 01, 02, etc? That is, with a leading zero on the single-digit integers.

3) Is there a way to use a range rather than an explicit numbering scheme--that is, mkdir /mydir/{00-49}, rather than having to type out each of the numbers? Us lazy sysadmins don't like to type, ya know :-)

While I can test on and even feasibly crash and reboot the system on which I'm working, I don't have that luxury with the filesystems I need to mount. So I'd rather go into it forearmed instead of just trying it out and hoping for the best...

Thanks for any help!

Jason
7 REPLIES 7
Hazem Mahmoud_3
Respected Contributor
Solution

Re: bash scripting tricks

Now, I haven't tested this out, but something along these lines should work:

i=00
for file in /mydir/*
do
mount server:/vol/vol1/$i /mydir/$i
i=$i+1
done


-Hazem
Hazem Mahmoud_3
Respected Contributor

Re: bash scripting tricks

Sorry, but you would have to use ((i=$i+1)) instead of i=$i+1.
(no points for this please)

-Hazem
Jason W. Neiss
Valued Contributor

Re: bash scripting tricks

That's actually pretty good. I have a tendency to get stuck thinking about style ("Oh, look! Here's a kewl new trick I can use!") which blinds me to the obvious sometimes.

I'd still like an answer to the questions, but since you cut right through the BS and solved the -problem-... :-)

Jason
Hazem Mahmoud_3
Respected Contributor

Re: bash scripting tricks

One more item I forgot to take into consideration was your question about adding a leading zero to single digits. Here is how the final script should look like to take that into consideration:

i=00
for file in /mydir/*
do
mount server:/vol/vol1/$i /mydir/$i
i=$i+1
if [ $i -lt 10 ]
then
i=0$i
fi
done

(again, no points please since that was part of your original questions)

-Hazem
Hazem Mahmoud_3
Respected Contributor

Re: bash scripting tricks

Shoot I put in the wrong add function again. Sorry!!! Here is the right one:

i=00
for file in /mydir/*
do
mount server:/vol/vol1/$i /mydir/$i
((i=$i+1))
if [ $i -lt 10 ]
then
i=0$i
fi
done

(again, you know the drill, no points please. Sorry about that, I'm getting hungry and I think I'll go to lunch. Maybe that will fix my brain:))

-Hazem
Jason W. Neiss
Valued Contributor

Re: bash scripting tricks

Good calls.

Of course, now we're getting away from the 'lazy sysadmin don't wanna type' reason for the original question... ;-)

Jason
Jason W. Neiss
Valued Contributor

Re: bash scripting tricks

From testing:

Brace expansion does not appear to work for the mount command. Issuing the command:

bash# mount server:/vol/vol1/{00,01,02} /mydir/{00,01,02}

returns a usage statement.

Still trying to figure out if ranges work with mkdir; so far, they don't.

Perhaps a script combining the two solutions might be the one to work. Ah, well.

Jason