Testing c-Shell optional parameters

January 31, 2012 at 04:08:26
Specs: Linux/Unix
I am stuck on testing for Optional parameters in a c-shell script.

this what I have so far
#!/bin/csh

if($1 == "") then
echo "This shell script compresses files with a specific extension"
echo "Call syntax: minimize <suffix_name> <zipprog> <Size>
"
echo "Example: minimize doc"
exit
endif

if ($2 == "") then
set $3 = `compress`
endif

echo $1
echo $2
echo $3
echo $4
foreach file(`\ls`)
#if ($file =~ *.$1) then
if (-f $file && $file =~ *.$1) then
set size = (`wc -c $file`)
if ($size[1] < $4 ) then
$3 $file
endif
endif
end

the second If statement is failing with a missing File name error. Not sure what I am Missing since the first if statement works correctly. If I comment out the 2nd if, the script will work as it should with all the parameters filled in.
the object is to supply a directory, a list of file types, and the script will compress the those files. the optional part is to let the script know the size of the files to compress and which compression program to use. it should default to compress,

minimize -s 500 -p gzip /etc/host txt

thanks


See More: Testing c-Shell optional parameters

Report •

#1
January 31, 2012 at 10:11:33
The reason you are getting a missing filename error is because with command line substitution you are trying to set the contents of the compress command to $3:

set $3 = 'compress`

Instead, build the command as a string and then execute it like in this test stub:

#!/bin/csh

foreach file(`ls`)
   set command='ls -l '$file
   $command
end

# end test stub

Sometimes, if the command string is complicated is complicated, instead of this:

$command

it must be eval'ed:

eval $command



Report •
Related Solutions


Ask Question