Replace the words in the file to the words that user type?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace the words in the file to the words that user type?
# 1  
Old 07-20-2014
Replace the words in the file to the words that user type?

Hello,

I would like to change my setting in a file to the setting that user input.

For example, by default it is

Code:
ONBOOT=ON

When user key in "YES", it would be

Code:
ONBOOT=YES

--------------

This code only adds in the entire user input, but didn't replace it.

How do i go about replacing it?

Code:
echo "ONBOOT="$USERINPUT >> /etc/myfile

Regards,

Malfolozy
Moderator's Comments:
Mod Comment CODE tags are for sample input and output as well as for code.

Last edited by Don Cragun; 07-20-2014 at 01:16 AM.. Reason: Add missing CODE tags.
# 2  
Old 07-20-2014
If this is the only thing in the file, change:
Code:
echo "ONBOOT="$USERINPUT >> /etc/myfile

to:
Code:
echo "ONBOOT=$USERINPUT" > /etc/myfile

# 3  
Old 07-20-2014
Hello,

Basically, the file that i wanted to edit is the /etc/sysconfig/network-scripts/ifcfg-eth0. Therefore, there are more than just one thing inside.

Regards,

Malfolozy
# 4  
Old 07-20-2014
One simple way that should work on any UNIX or Linux system is (as long as the file you're editing is a non-empty text file):
Code:
#!/bin/ksh
val=${1:-YES}
file=${2:-/etc/sysconfig/network-scripts/ifcfg-eth0}
ed -s "$file" <<-EOF
	g/^ONBOOT=/d
	i
	ONBOOT=$val
	.
	w
	q
EOF

This will replace an ONBOOT= line if there is one and add the desired line if there isn't one.
# 5  
Old 07-20-2014
Hello, the code does'nt seems to be working as it doesn't change anything in the file.

Code:
whiptail --title "Abc" --menu "Your choice" 16 78 5 \
"1)" "ONBOOT" 2>somefile

CHOICE=$(cat somefile)
case $CHOICE in
"1)") ANSWER=$(whiptail --inputbox "Yes or no?" 8 78 --title "Example" dialog" 3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 0 ]; then 

file=${2:-/etc/sysconfig/network-scripts/ifcfg-eth0}
ed -s "$file" <<-EOF
	g/^ONBOOT=/d
	i
	ONBOOT=$ANSWER
	.
	w
	q
EOF

else

echo "ABC"

fi;;
esac

my code are as such, can you spot the mistake?

Regards,

Malfolozy
# 6  
Old 07-20-2014
Instead of asking us to guess what is wrong with your script, why don't you show us (in CODE tags) the diagnostics that were printed when you ran your script? What output did you get? What output were you expecting? How did you respond to the whiptail commands? (In other words, why not give us the very basic information needed to determine what went wrong?)

You are not checking the exit status of the whiptail command on lines 1 and 2.

You have mismatched double quotes on line 6.

What is the 2nd argument you are passing to your script? How are you invoking your script? What OS are you using? If you aren't specifying a shell when you invoke your script, what is the default shell for your OS?

I would guess that the mismatched quotes on line 6 are your immediate problem and the other issues may be important after you fix line 6.

Is my guess close???

Please get into the habit of using indentation to make the structure of your code obvious.

The whiptail man page says that if the user selects No or Cancel from several of the options that give you choices, the exit status is 1; and if there is an error the exit status is -1. Despite what the whiptail man page says, there is no such thing as a negative exit code. If you want to explicitly look for the exit code that the whiptail man page says is -1, look for 255 instead. Nonetheless, get into the habit of checking for errors from the commands you run.

Also, get into the habit of verifying that user supplied input is appropriate rather than assuming that anything a user types is always correct.

You might want to try something more like:
Code:
#!/bin/ksh
whiptail --title "Abc" --menu "Your choice" 16 78 5 \
	"1)" "ONBOOT" 2>somefile
exitstatus=$?
if [ $exitstatus -eq 0 ]
then	read CHOICE < somefile
elif [ $exitstatus -eq 1 ]
then	echo 'User selected Cancel'
	exit 0
else	echo '1st whiptail failed'
	exit 1
fi
case $CHOICE in
"1)")	ANSWER=$(whiptail --inputbox "YES or NO?" 8 78 "YES" \
		--title "Example dialog" 3>&1 1>&2 2>&3)
	exitstatus=$?
	if [ $exitstatus -eq 0 ]
	then	# You should verify that $ANSWER is legitimate here before using it.
		file=/etc/sysconfig/network-scripts/ifcfg-eth0
		ed -s "$file" <<-EOF
			g/^ONBOOT=/d
			i
			ONBOOT=$ANSWER
			.
			w
			q
		EOF
		# You should check the exit status from ed here.  If ed fails the
		# diagnostic it prints could just be "?" which could leave users
		# wondering what happened.  So, if ed failed, print a clear
		# diagnostic message.
	elif [ $exitstatus -eq 1
	then	echo "ABC"
		exit 0
	else	echo '2nd whiptail failed'
		exit 1
	fi;;
esac

Unfortunately, since I don't have whiptail on my system, this is partially untested code.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

How to replace some specific words from file?

I have the file like this. cat 123.txt <p> <table border='1' width='90%' align='center' summary='Script output'> <tr><td>text </td> </tr> </table> </p> I want to replace some tags and want the output like below. I tried with awk & sed commands. But no luck. Could someone help me on this? ... (4 Replies)
Discussion started by: thomasraj87
4 Replies

3. Shell Programming and Scripting

How to replace words in file?

Hi Guys, I have a text where we used Ram in 10 times now I want replace all Ram words by Shyam word then how to do it. (6 Replies)
Discussion started by: aaditya321
6 Replies

4. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

5. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

6. Shell Programming and Scripting

Splitting concatenated words in input file with words from the same file

Dear all, I am working with names and I have a large file of names in which some words are written together (upto 4 or 5) and their corresponding single forms are also present in the word-list. An example would make this clear annamarie mariechristine johnsmith johnjoseph smith john smith... (8 Replies)
Discussion started by: gimley
8 Replies

7. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

8. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

9. UNIX for Dummies Questions & Answers

sed replace words in file and keep some

lets see if i can explain this in a good way. im trying to replace some words in a file but i need to know what the words are that is beeing replaced. not sure if sed can do this. file.name.something.1DATA01.something.whatever sed "s/./.DATA?????/g" need to know what the first . is... (2 Replies)
Discussion started by: cas
2 Replies

10. Shell Programming and Scripting

How to replace a word with a series of words in a file

Hi, I have a Template file 'TL.body' which says as follows: "There are no <FILENAME> files on the server. " The missing file names are identified and stored in a variable. For Eg: MISSFILE="abc.txt def.txt xyz.txt" I want the values of MISSFILE variable to be replaced against... (2 Replies)
Discussion started by: brap45
2 Replies
Login or Register to Ask a Question