Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bash script

Status
Not open for further replies.

zgrande

Programmer
Mar 18, 2002
15
0
0
BR
Hi,

I'm newbie on Unix, and I need to create a script renaming some files. The loop using the "for" command works fine unless there isn't a file to rename. For example:

FLD="/test/fl*"
for FL in $FLD
do
mv $FL /test/file.txt
#other statements....
done

So, if there's no file in the folder the variable FL is assigned with the search string (/test/fl*). I already tried to check if the file really exists:

FLD="/test/fl*"
for FL in $FLD
do
if test '-e $FL'
then
mv $FL /test/file.txt
else
echo "File not found"
fi
done

but the test doest work, it always execute the "then" option.

I wonder if this is an AIX's (ver 5L) particularity, because according to the tutorials I've read the test should work fine on Linux.

Regards,
ZGrande

And sorry for my bad english.
 
The default version of ksh (in turn, the default shell) on AIX does not support the "-e" test. Use "-a". It does the same test, but is listed in Korn's book as obsolete.

Or you could use ksh93, which does support "-e".


Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Hi Zgrande!

You might want to test the status of $FLD before the loop, and when $FLD is an empty string, skip the whole loop.

Code:
FLD="/test/fl*"

if [ -z "$FLD" ]; then
  for FL in $FLD
  do
       mv $FL /test/file.txt
  done
fi


By the way, this script will destroy all files from "/test/fl*" but the very last one. This is because you move them one-by-one to the same file, overwriting the previous in each loop.

If you want to concatenate files to a single one, you might do it like this:
cat /test/fl* >> /test/file.txt
rm /test/fl*

--Trifo
 
man basename dirname

for file in "ab/cd ef/gh"
do
dir=`dirname $file`
base=`basename $file`
mv $file $dir/$base.zzz.txt
done
 
make sure you are actually using bash. otherwise you need to kornify things, as per previous suggestions. you can, of course, compile bash on AIX if you want.

IBM Certified -- AIX 4.3 Obfuscation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top