0

My assignment asks that:

Create a directory ~/UnixCourse/scriptAsst. Turn the two-line version, above, of the substitution commands into a shell script, subst1 taking three parameters: the string to be replaced the string with which to replace it the name of the file in which to make the substitution.

For example,

`~/UnixCourse/scriptAsst/subst1 foo bar myFile.txt` 

should replace all occurrences of foo in the file myFile.txt by bar, leaving the original file as myFile.txt.bak.

Similarly,

`~/UnixCourse/scriptAsst/subst1 abc "" aardvark.dat` 

should remove (replace by the empty string) all occurrences of abc in the file aardvark.dat with nothing, leaving the original file as aardvark.dat.bak.

My code that I came up with is:

#!/bin/bash set p1 = "$1" shift set p2 = "$1" shift set p3 = "$*" echo $p1 echo $p2 echo $p3 if grep "$p1" "$p3" > /dev/null; then mv "$p3" "$p3.bak" sed "s/$p1/$p2/g" "$p3.bak" > "$p3" fi 

When I try to run:

./subst1 foo bar myFile.txt 

I keep getting:

grep: : No such file or directory 

Please help!! What am I doing wrong??

4
  • 1
    What does the script print for the echo $p1...? Commented Dec 3, 2011 at 16:59
  • 2
    Did you copy and paste that exactly? The error looks like you have a stray : after your grep command. Commented Dec 3, 2011 at 17:07
  • That paste is exactly what I have Commented Dec 3, 2011 at 17:16
  • NVM, it's grep not being able to find a file with no name (""). Apparently $p3 isn't getting the filename. Commented Dec 3, 2011 at 17:31

3 Answers 3

1

This is how you set variables:

p1="$1" shift p2="$1" shift p3="$1" 

or in this case simply:

p1="$1"; p2="$2"; p3="$3" 

Note:

  • try to use meaningful variable names, or simply use $1 directly.
  • there is grep -q so you don't have to redirect standard output.
Sign up to request clarification or add additional context in comments.

1 Comment

ok, this code helped. Now I get the error: subst1: Could not find the backup file (test 3). It's something for class, once I get this, Ill flow with everything else.
1

You don't actually need to do anything.

sed -i.bak "s/$1/$2/g" "$3" 

Comments

0

or use the parameters directly...

#!/bin/bash echo $1 echo $2 echo $3 if grep "$1" "$3" > /dev/null; then mv "$3" "$3.bak" sed "s/$1/$2/g" "$3.bak" > "$3" fi 

5 Comments

ok, this code helped. Now I get the error: subst1: Could not find the backup file (test 3). It's something for class, once I get this, Ill flow with everything else.
that code worked for me... what's the name and content of your input files?
im entering: ./subst1 foo bar myFile.txt for my tests
strange, I use the exact same command. And your myFile.txt exists in the same directory and contains some text (preferably some "foo"s :))?
ok, I see it now. I have to find out why the test isn't working. Because the .bak file is in the same directory for me too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.