I'm trying to create a ksh script that will ask the user for the port number. $PORT1 is the variable I want to use that will contain whatever numbers the user inputs. The script would edit ports.txt file, search and delete "./serv 110.1.0.1.$PORT1 200;=3" .
So if the user types 50243 then the script would search and delete "./serv 110.1.0.1.50243 200;=3" so far no luck on accomplishing this. Please help
This is what I have so far:
printf "Enter port to be removed from monitoring: "
read PORT1
ex /home/user/ports.txt <<"EOF" > /dev/null 2>&1
%s!"./serv 110.1.0.1.$PORT1 200;=3"!!g
w
q
EOF
Variable expansion does not happen inside a quoted here document.
I don't think it is a good idea to throw away diagnostic messages from ex . If you just don't want to see the informative messages and the read and write counts, use the -s option to suppress them.
Try something more like:
printf "Enter port to be removed from monitoring: "
read PORT1
ex -s /home/user/ports.txt <<EOF
%s!"./serv 110.1.0.1.$PORT1 200;=3"!!g
w
q
EOF
Note that the double quotes surrounding EOF marked in red in your script above have been removed.
Note that with an unquoted here document, you need to be careful with dollar signs in the text. For example, the ex command to delete the last line ( $d ) would be expanded to the contents of the shell variable d . That isn't a problem in this script, but is something to be considered in scripts like this. You can avoid that problem by escaping the dollar sign ( \$d ) or by separating the address from the command letter ( $ d ).