[svlug] Shell Programming Question
Ivan Passos
lists at cyclades.com
Fri Jul 21 10:46:02 PDT 2000
On Fri, 21 Jul 2000, Sean Cheng wrote:
> Hi, I'm learning Shell. I'd like to write a shell script to help me backup
> files, but I can't figure out 2 errors.
>
> Here is the script
> ------
> #get the filename
> for file in `ls -l|gawk -F" " '{print &9}'`
> #separate the sub-filename, if sub-filename matches prt then
> #backup the file
> subname=`echo $file|cut -d"." -f2`
> test "$subname"="prt" && {echo $subname;echo $file}
Ok, try the following:
#!/bin/sh
for file in `ls`
do
subname=`echo $file | cut -d"." -f2`
if test "$subname" = "prt" ; then
echo $subname ; echo $file
fi
done
Note the spaces in the test command, they are important.
Also, in order to define that the condition for the "echo" commands to
execute is the "test" result, you need to use the "if" construction.
> Another thing is that I can't write
> test "$subname"="prt" as
> ["$subname"="prt"]
> because I'll get the error message like
>
> ./backup: [prt=prt]: command not found
> ./backup: [drw=prt]: command not found
There _must_ be spaces around the '=' and spaces separating the '[]', as
in:
[ "$subname" = "prt" ]
Hope this helps.
Regards,
Ivan
More information about the svlug
mailing list