I have a script that creates folders depending on what variables i enter when calling the script. This all works as expected but what i want to do is add some additional data into another file so that other scripts are aware of this file.
I've found the following sed command which works to my needs. It enters the given details into a given line
sed "36a\mv -v $foo/"$SHOW"\ $bar/$SHOW/" test2.sh > test3.sh
the problem i'm having is that when the sed command has run, $SHOW displays still in the new file as $SHOW. Is there something i'm doing wrong as to why $SHOW wont "resolve" to the text i need to to be?
also is there an easier way to output the results of SED back into the original file? Instead of having to manipulate it in such a way that i end up with the original file name?
So my initial script has a case command in which calls up functions according to which option i select. Within one of those functions i have the sed code which is to take the input of $SHOW and apply it into the file.
so say $SHOW = test foobar
then the sed command should should run like:
sed "36a\mv -v $foo/"test foobar"\ $bar/test foobar/" test2.sh > test3.sh
So in tesh3.sh i should see around line 36 the following:
mv -v $foo/"test foobar"\ $bar/test foobar/
but i always get $SHOW appear in the final file and it never takes the input from $SHOW and replaces it. $foo and $ bar are part of the test3.sh script so all they need to do is be entered into the test3.sh file.
Other parts of the function works when $SHOW is called its just sed that doesn't like the input.
If what you are trying to do is to create a directory and create a script (test3.sh) that is a copy of another script (test2.sh) with an mv command added after line 36 then I think the following does what you want. You haven't said what shell you're trying to use for this script, so I'm assuming a standards conforming shell such as ksh or bash. (I used ksh while I was testing this script.) Assuming that is correct, there are a couple of problems:
You can't have a minus sign in a function name. (I changed create-new to create_new to solve this problem.)
It is hard to get the quotes correct using sed to transform test2.sh into test3.sh. (In this case I found it much easier to use a here-document with ed instead of trying to get the editing command arguments specified correctly with sed.)
I added an additional case to provide a usage message (since I kept forgetting to add arguments while I was testing the script.
I added matching ('s to the cases in your switch because I use showmatch while editing scripts and I find it easier to read when I do that.
Anyway, does this do what you're trying to do?
#!/bin/ksh
bar=to #Added because the script needs a value for bar
foo=from #Added because the script needs a value for foo
me=${0##*/} #Added for use in the usage message
SHOW="$2"
create_new(){
mkdir "$bar/$SHOW"
ed -s test2.sh <<-EOF
36a
mv -v "$foo/$SHOW" "$bar/$SHOW/"
.
w test3.sh
EOF
}
case "$1" in
(create)
create_new ;;
(remote)
remove ;;
(*) printf "Usage:\t%s create file\n\t%s remove Who Knows What\n" \
"$me" "$me" ;;
esac
If not, you're going to have to actually show us exactly what you want to appear in test3.sh rather than showing us the commands that you're using to create a test3.sh that does not contain what you want.
I also note that your script has a case "remote" that calls the undefined function "remove". I assume one of these is misspelled, but since you haven't given us any indication of what should happen when $1 is not "create", I left it as is.
You are telling us that (unquoted) $foo and $bar are being expanded, while (quoted) $SHOW is not.
Is the mkdir one line above working correctly, then?
Why don't you post an execution log with the -x option set?
I took out the "" which was wrapped around $SHOW within sed
The only thing i have now is that when trying to run the test3.sh it fails due to the spaces as the file system uses "\" at the end of words with spaces.
Probably best to start a new thread to solve this other interesting problem. Include the input, expected output, and what you have tried, using code tags.