0

I am very new in shell scripting, here is a simple program to create,copy,append operation in file,but I do not know what is my error in this code

Q1.sh

echo "1.create a file\n"
echo "2.copy content of files\n"
echo "3.append files\n"

read ch

if $ch -eq 1
then
read file
cat > $file
fi
if $ch -eq 2
then
read file1 file2
cp $file1 $file2
fi
if $ch -eq 3
then
read file1 file2
cat $file1 >> $file2
fi

error When i press 1 it says 1 not found

Vikas Gautam
  • 441
  • 8
  • 22
  • 2
    `$ch -eq 3` is running `1 -eq 3` which is trying to run a command called `1`. Probably what you *actually* want is the command `test`: `test "$ch" -eq 3`, or its shorthand `[ "$ch" -eq 3 ]` (`[` is an alias to `test`). – Charles Duffy Aug 08 '17 at 17:25
  • As an aside -- we ask when asking a question that you build the smallest possible code that produces the same problem -- so if the `echo`s can be taken out, do so; if you can hardcode `ch=1` instead of running `read ch`, do so; if you only need one test instead of three, do so; etc. See the documentation on building a [mcve]. – Charles Duffy Aug 08 '17 at 17:28
  • Comparisons can be tricky for new users. If you're _actually_ writing a bash script (on some systems `sh` actually points to `bash`), this may be a good resource for you: http://tldp.org/LDP/abs/html/comparison-ops.html – Michael Wu Aug 08 '17 at 17:31
  • thanks now it is working, one more question, is read stores in string format? – Vikas Gautam Aug 08 '17 at 17:42

0 Answers0