There are a number of problems with your script. The first and most important is that you're using csh
. Don't. Among its many failings as a scripting language, csh
doesn't support $()
, you need backtics. You also need cat
in order to concatenate files, you can't just execute the file as though it were a program. Not if you want to print its contents, anyway. You also have the if
syntax wrong. Finally, you don't need else if
. Since you only have two actions to perform, what's the point of checking for three different conditions? All conditions but one will do the same thing, so just use if ... else
.
A working version of your script in csh
would be:
#!/home/terdon/bsd-csh -f
echo ENTER a file name
set fl1 = $<
echo ENTER a second file name
set fl2 = $<
set var1 = `wc -c < "$fl1"`
set var2 = `wc -c < "$fl2"`
echo "V: $var1 $var2"
if ( ! -e "$fl1" && ! -e "$fl2" ) then
echo File does not exist
echo -n Please enter a vaild file name
else if ( "$var1" > "$var2" ) then
cat "$fl2" >> "$fl1"
else
cat "$fl1" >> "$fl2"
endif
Here's a script with the same functionality in bash, just to illustrate what you're missing:
#!/bin/bash
read -p "ENTER a file name:" fl1
read -p "ENTER a second file name:" fl2
[[ -e "$fl1" && -e "$fl2" ]] ||
echo "File does not exist, please enter a vaild file name" && exit;
if [[ $(wc -c "$fl1") -lt $(wc -c "$fl2") ]]
then
cat "$fl2" >> "$fl1"
else
cat "$fl1" >> "$fl2"
fi