Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I would like to modify a file to replace keys, suppose these keys are in the file as:

42NM
52NM
23NO
XNNM

I would like to replace anything with NM, with the word "Okay".

 1 #!bin/bash/
 2 
 3 if [ -f KeyFile]
 4 then
 5         sed 's/[0-9][0-9]NM/Okay/g' KeyFile
 6 else
 7         echo "File does not exist or cannot be found."
 8 fi
 9 
10 exit 0

I ran the command:

chmod a+x FindKeys

and then, when I attempt to run the script, I get:

-bash-3.00$ ./FindKeys
-bash: ./FindKeys: bin/bash/: bad interpreter: No such file or directory

I seem to have two problems, one the script file is not running correctly, and two the sed command is not working.

share|improve this question
    
sed -i 's/NM/Okay/' KeyFile –  Cyrus Aug 18 at 19:26
3  
your first line is missing a / , it should be: #!/bin/bash –  Alex Stragies Aug 18 at 19:29
    
If you write the command which bash in the CLI, what gets printed? –  BinaryZebra Aug 20 at 5:15
    
When I do which bash, I get /bin/bash. –  Juan Davila Aug 26 at 14:06

1 Answer 1

up vote 4 down vote accepted

Line 1: Your hashbang line is not correct, use:

#!/bin/bash

Line 3: Take care with the test utility (it needs a space before the closing ]):

if [ -f KeyFile ]

Line 5: In the sed command, use -i to activate in-place editing of sed, else the edits are only printed to the stdout:

sed -i 's/[0-9][0-9]NM/Okay/g' KeyFile
share|improve this answer
    
I have made those changes though I still get:-bash: ./FindKeys: /bin/bash/: bad interpreter: Not a directory –  Juan Davila Aug 18 at 19:41
    
@JuanDavila sry, a typo, see my edit: #!/bin/bash, without the ending slash –  chaos Aug 18 at 19:43
    
Additional advice (which wasn't requested but I'm giving anyway): on line 7, I suggest redirecting the echo to stderr (i.e. append >&2), and exit with a non-zero status. This is really useful for composition (calling this script from another, or from a Makefile, etc.). –  Toby Speight Aug 19 at 7:05
    
I was able to get this to work, running the command directly in the shell prompt, though for whatever reason I still get the "bad interpreter error" when using it in a script. –  Juan Davila Aug 26 at 14:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.