1

Ok so I am using Bash to call a synFlood python script for class. I want to incorporate user input into this so I used Bash (since I was not allowed to use python). I have called my source and target IP's from the user and stored them and then made new variable for the entire string inside of the python script to replace with sed.

Currently my command is working and replacing the string. But when I end the script the string I said to replace is still the original string.

EXAMPLE:

sourcepath="src = $source" #set user source IP to full path for python script
targetpath="tgt = $target" #set user target IP to full path for python script

sed -e "s/^src.*/$sourcepath/" #replace the src string with sourcepath variable
sed -e "s/^tgt.*/$targetpath/" #replace the tgt string with targetpath variable

python syn.py

Sed replaces both strings in my python file to what the user specifies; as it shows it in the terminal window (for testing anyways, final script will hide this output). But when I check the file after ending the script nothing was changed and the default values are still there.

Does sed just change the values only in the context of the script while it's running? If so, when I call the python script in the follow on line will it use the values currently in there from my sed changes or will it use the default files in the python script?

Thanks in advance for the help and clarification.

undecisive
  • 59
  • 4

3 Answers3

1

The sed command doesn't modify files. It reads them in and copies them to standard output with the modifications made, but the original file is untouched.

If you have the GNU version of sed, there's a flag you can turn on to enable in-place modification: sed -i.bak s/old/new/ filename will modify the file (after creating a backup) instead of just making a copy on standard output with the changes.

If you don't have the GNU version, you can still redirect the output into a file and copy it into place:

cp -p filename filename.bak
sed -e s/old/new/ filename.bak >filename
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • 2
    Note that `sed -i` does **not** create a backup; it does so only if you specify `sed -i[SUFFIX]`. Test your `sed` command without `-i` until you are *sure* you got it right... – DevSolar Jul 15 '15 at 15:20
  • Ahh, this makes more sense now. I just tried it with the -i switch and declared a .bak and it worked but I ran into several issues trying to get the single quotes to work as well. instead of doing a ton of escapes i swapped over to \x27 and it worked flawlessly. Thanks for the clarification on how sed prints to stdout and not to a file without the -i switch. – undecisive Jul 15 '15 at 17:33
  • Please "accept" an answer if it solved your problem, @undecisive.. just click the checkbox (on this and either of your other question[s] that received useful answers). – Mark Reed Jul 20 '15 at 13:44
0

That snippet is clearly not your original script as it doesn't work. Those sed commands have no data to operate on (no standard input and no filename argument).

Assuming you actually have something more like this:

sed -e "s/^src.*/$sourcepath/" syn.py #replace the src string with sourcepath variable
sed -e "s/^tgt.*/$targetpath/" syn.py #replace the tgt string with targetpath variable

then the problem is that you aren't actually changing the file on disk. You are just having sed read the file and spit the changed contents to standard output.

You need to redirect that to a file (not the same one that doesn't work) or use the -i flag to tell sed to modify the file "in-place".

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Sorry if it was confusing, I just took a snippet from the code instead of posting the all lines of the code up here. – undecisive Jul 15 '15 at 17:16
0

By saying

sed -e "..." file

you are performing the replacement and the output gets displayed in your screen (stdout). This is the default behaviour of sed.

If you want the file to be updated with the new content, you need to mention it. For this, you have two options:

  • In place editing

    sed -i.bak "..." file
    

this will replace the file and create a file.bak backup file with the original content. Check man sed because depending on the distributions the syntax may vary (OSX or GNU). Basically, in OSX you need to say sed -i '' if you don't want to create a backup file. See In-place edits with sed on OS X for more details.

  • Redirect to another file:

    sed "..." file > tmp_file && mv tmp_file file
    

That is, instead of printing to stdout, you print to a temporary file that then gets renamed to the original one.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Thank you for the clarification on how it operates and for providing two ways of accomplishing the task of writing the changes to the file. I utilized the -i switch because I thought it would be easier and less code in the script. – undecisive Jul 15 '15 at 17:35
  • Nice to read that, @undecisive Remember you can accept an answer if it worked to you. – fedorqui Jul 16 '15 at 08:12