LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2012, 02:24 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
more BASH help, trying to echo >> /etc/hosts from a file


I am trying to create a setup type script that will interact with a user to gather data, then populate the /etc/hosts file with the IP address provided.

Code:
#!/bin/bash

# this is a test

CONFFILE="${HOMEDIR}/.nasbackup"
firstTime=0

if [ -f ${HOMEDIR}/.nasbackup ]
then
	echo "${CONFFILE} | tee -a ${LOG}
else
	echo "Creating ${CONFFILE}" | tee -a ${LOG}
	sleep 1
	echo -n "active." >${CONFFILE}.tmp
	firstTime=1
fi
echo "ftp.rx30backup.com" >>${CONFFILE}.tmp

echo "Running setup for NAS backup with e-mail notificatoin."
echo "Previous configurations will be overwritten."
echo -n "Continue? (Y/N) :"
	read ANSWER
if [[ ${ANSWER} != [Yy] ]]
then
	exit 1
fi
echo -n "Enter the user name for the NAS device: "
read USER
echo -en "\t${USER}" >>${CONFFILE}.tmp
echo -n "Enter the password for the NAS device: "
read PASS
echo -en "\t${PASS}" >>${CONFFILE}.tmp
echo -n "Enter the IP of the NAS device: "
read IP
echo -en "\t${IP}" >>${CONFFILE}.tmp
echo -n "Enter the path for the backup"\r "ex: disk1/rx30   :"
read RDIR
echo -en "\t${RDIR}" >>${CONFFILE}.tmp
echo "Do you want to configure the backup program to restart"
echo -n "Vrtual Pharmacist after the backup is complete?(Y/N) "
read VP

if [[ ${VP} = [Yy] ]]
then
	echo -en "\tVirtRPH" >>${CONFFILE}.tmp
fi

if [ ${firstTime} -eq 1 ]
then
	echo "First time setup for NAS.  Enter the root password when prompted."
	echo -n "Typically this is 1234"
	fi
mv ${CONFFILE}.tmp ${CONFFILE}

grep -w "ftp.rx30backup.com" /etc/hosts || { echo "Adding hosts entry" | tee -a ${LOG}
		set - `cat ${CONFFILE}`
		echo "User name is $2    Password is $3    IP address is $4    Path is $5    5 is $6"
                 su - -c 'echo "/$4 $1">> /etc/hosts
		
echo "  "
echo "Setup is complete."
echo "  "
echo -n "Do you wish to read the help menu? (Y/N) :"
read ANSWER2
if [[ ${ANSWER2} -eq [Yy] ]]
then
	USAGE | less
	sleep 1

else

echo -n "Do you wish to run a backup now? (Y/N) :"
read ANSWER3
	if [[ ${ANSWER3} != [Yy] ]]
	then
		exit 2
	
	fi
fi
when i manually create the .backup file (just using that as an example) then set - `cat .backup` i can echo out the data i want from the file and it reports exactly as i would expect.

The problem is when I su -

Code:
su - -c 'echo "$4 $1">> /etc/hosts
i am prompted for the root p/w as i would expect, but when i cat /etc/hosts there is nothing there. yet if i:

[code]echo "$4 $1" i am presented with:
Code:
192.168.0.60 ftp.rx30backup.com
why is this NOT being populated into /etc/hosts.

oh and yes i can:
Code:
echo "192.168.0.60 ftp.rx30backup.com >> /etc/hosts
and that works without issue. what am i doing wrong?

Last edited by lleb; 08-13-2012 at 02:26 PM.
 
Old 08-13-2012, 02:28 PM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
[ray@ray /stuff/backup]$ cat .nasbackup 
rx30	rx30	192.168.0.60	disk1/rx30	ftp.rx30backup.com
[ray@ray /stuff/backup]$ vi .nasbackup 
[ray@ray /stuff/backup]$ cat .nasbackup 
ftp.rx30backup.com	rx30	rx30	192.168.0.60	disk1/rx30
[ray@ray /stuff/backup]$ cat /etc/hosts
127.0.0.1		localhost.localdomain localhost
::1		localhost6.localdomain6 localhost6
10.10.0.128 	rx30wiki.rx30.com
10.10.0.106	str.rx30.com
10.10.0.105	fileserver
10.10.		mylan
71.43.246.158	sisconsulting.biz
[ray@ray /stuff/backup]$ set - `cat .nasbackup`
[ray@ray /stuff/backup]$ echo $1 $4
ftp.rx30backup.com 192.168.0.60
[ray@ray /stuff/backup]$ echo $4 $1
192.168.0.60 ftp.rx30backup.com
[ray@ray /stuff/backup]$ su - -c 'echo "$4 $1" >> /etc/hosts'
Password: 
[ray@ray /stuff/backup]$ cat /etc/hosts
127.0.0.1		localhost.localdomain localhost
::1		localhost6.localdomain6 localhost6
10.10.0.128 	rx30wiki.rx30.com
10.10.0.106	str.rx30.com
10.10.0.105	fileserver
10.10.		mylan
71.43.246.158	sisconsulting.biz
 
[ray@ray /stuff/backup]$
Code:
[ray@ray /stuff/backup]$ su - -c 'echo "192.168.0.60 ftp.rx30backup.com" >> /etc/hosts'
Password: 
[ray@ray /stuff/backup]$ cat /etc/hosts
127.0.0.1		localhost.localdomain localhost
::1		localhost6.localdomain6 localhost6
10.10.0.128 	rx30wiki.rx30.com
10.10.0.106	str.rx30.com
10.10.0.105	fileserver
10.10.		mylan
71.43.246.158	sisconsulting.biz
 
192.168.0.60 ftp.rx30backup.com
So why does one work, while the other does not?
 
Old 08-13-2012, 03:06 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
One of the subtle differences between ' and "...

Variable expansion will occur within double quotes ("), but not within single quotes ('). Your $1 and $4 are nested within single quotes, so it is being passed as a literal "$4 $1" to su's -c. Eg:
Code:
$ a=stuff
$ b=things
$ echo "$a $b"
stuff things
$ echo '$a $b'
$a $b

SU runs in a separate shell, so when it executes the command and tries to expand $1 and $4, those variables are empty (they were defined in the original script, but not in this subshell), which is why nothing happens. What you need to do is modify the script to expand the $1 and $4 variables BEFORE they make it inside the su subshell. There are many ways to do this, such as
Code:
command='echo '"$4 $1"' >> /etc/hosts'
su - -c "$command"
 
1 members found this post helpful.
Old 08-13-2012, 03:26 PM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
many thanks.

Code:
[ray@ray /stuff/backup]$ command='echo '"$4 $1"' >> /etc/hosts'
[ray@ray /stuff/backup]$ cat /etc/hosts
127.0.0.1		localhost.localdomain localhost
::1		localhost6.localdomain6 localhost6
10.10.0.128 	rx30wiki.rx30.com
10.10.0.106	str.rx30.com
10.10.0.105	fileserver
10.10.		mylan
71.43.246.158	sisconsulting.biz
 
[ray@ray /stuff/backup]$ su - -c "${command}"
Password: 
[ray@ray /stuff/backup]$ cat /etc/hosts
127.0.0.1		localhost.localdomain localhost
::1		localhost6.localdomain6 localhost6
10.10.0.128 	rx30wiki.rx30.com
10.10.0.106	str.rx30.com
10.10.0.105	fileserver
10.10.		mylan
71.43.246.158	sisconsulting.biz
 
192.168.0.60 ftp.rx30backup.com
first time I read that I missed the extra ' ' surrounding the "" with the variables. for my understanding, we are using two pairs of ' ' because we are nesting a command within a the variable?

Last edited by lleb; 08-13-2012 at 03:29 PM.
 
Old 08-13-2012, 03:50 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
In all fairness, the double sets of quotes probably aren't necessary. You could try playing with them to see what happens. The important thing is that the variable expansion is outside of the single quotes. This way they're expanded before the call to su and not after.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
[SOLVED] echo to a file using bash binbash Programming 2 11-07-2010 05:37 AM
BASH: instad of echo-ing, I just want to assing to a bash variable... how?? rylan76 Linux - Newbie 9 11-28-2008 08:46 AM
Bash shell file... echo on? carlos_vcan Linux - Newbie 3 06-12-2008 10:10 PM
(bash) echo "#!/bin/bash" event not found - trying to generate profiles automatically jimieee Programming 9 05-03-2006 10:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration