ksh script trying to pass a variable to edit a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh script trying to pass a variable to edit a file
# 1  
Old 11-29-2014
Wrench ksh script trying to pass a variable to edit a file

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:
Code:
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

# 2  
Old 11-29-2014
You should consider sed:
Code:
sed -i -e "s!./serv 110.1.0.1.$PORT1 200;=3!!g" /home/user/ports.txt

(untested)
# 3  
Old 11-29-2014
i'm using aix and it doesn't support sed option -i
# 4  
Old 11-29-2014
then, create a temporary file:
Code:
sed -e 's/...//g' file > temp
mv temp file

You can use mktemp to create a temporary file, or something like:
Code:
file=/home/user/ports.txt
sed -e 's/....//g' ${file} > ${file}.$$
mv ${file}.$$ ${file}

Note that mktemp is guaranteed to be unique.
# 5  
Old 11-29-2014
Quote:
Originally Posted by seekryts15
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:
Code:
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:
Code:
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).
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 11-29-2014
Thank worked perfectly! Thank you for the informative info as well, much appreciated!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass and read an array in ksh shell script function.?

I'm able to read & print an array in varaible called "filelist" I need to pass this array variable to a function called verify() and then read and loop through the passed array inside the function. Unfortunately it does not print the entire array from inside the funstion's loop. #/bin/ksh... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. UNIX for Advanced & Expert Users

File edit script exceptions how to pass to script

i wrote script for if file exists script do file edit problem with the script get stuck not existing as for exit i mentioned exit 0 , and how to give the exception for script it should add ./script -- add hi ./script --add "hi how are you" now below script with case it is working for... (0 Replies)
Discussion started by: markjohn1
0 Replies

4. Shell Programming and Scripting

ksh script to edit a file using vi editor

I was wondering if it is possible to execute a script that will remove a certain search pattern from a file and save it? Manually I would just hit escape to enter command mode then search and delete the pattern "./srv 135.0.0.1.11111 210;=1" then save & exit the file vi command to search and... (3 Replies)
Discussion started by: seekryts15
3 Replies

5. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

6. Shell Programming and Scripting

Pass a DDL statement to a KSH script

I need to pass a DDL statement into a ksh script & parse the statement. What is the best way to pass a DDL statement into a KSH script. ---------- Post updated at 09:28 AM ---------- Previous update was at 07:35 AM ---------- if the name of the script is test.ksh test.ksh "ALTER TABLE... (12 Replies)
Discussion started by: gayathree
12 Replies

7. Shell Programming and Scripting

AIX ksh: how to pass variable to host shell

I have a script that "runs" a script. For example: ./runscript.ksh pcnmc01.ksh runscript puts pcnmc01.ksh into the background with log output going to the logfile. After executing the command, I get this output: Running script in the background: pcnmc01.ksh Logfile:... (2 Replies)
Discussion started by: Eben Yong
2 Replies

8. Shell Programming and Scripting

ksh variable pass to awk

I'm trying to store the response from a nawk command inside of a ksh script. The command is: text=$(nawk -F: '$1 ~ /${imgArray}/ {print $2}' ${etcDir}/captions.txt) From what I can tell, the imgArray variable is not being expanding when it is inside the single quote ('). Is there something I... (4 Replies)
Discussion started by: meman1188
4 Replies

9. Shell Programming and Scripting

edit a file using ksh

Hi, sample file looks like this.. <hp> <name> <detail>adsg</detail> ... ... </name><ft>4264</ft> </hp> I need to edit the last but one line. I want the format to be .. <hp> <name> <detail>adsg</detail> ... ... </name> (3 Replies)
Discussion started by: meghana
3 Replies

10. Shell Programming and Scripting

How to pass variable to SQLPLUS in a ksh script?

Hi, I am writing a ksh script which will use sqlplus to run a sql and pass 2 variables as the SQL request. In the ksh script, I have 2 variables which are $min_snap and $max_snap holding 2 different numbers. Inside the same script, I am using SQLPLUS to run an Oracle SQL script,... (6 Replies)
Discussion started by: rwunwla
6 Replies
Login or Register to Ask a Question