LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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
 
LinkBack Search this Thread
Old 08-08-2012, 01:27 AM   #1
*unix*
LQ Newbie
 
Registered: Aug 2012
Posts: 1

Rep: Reputation: Disabled
Question New Unix/Linux user - trying to a write a bash script to change the hostname on RHEL


Hi All,

I am new to Unix/Linux and I am currently in the process of learning bash shell script. I am trying to write a script, where a user/root can change the hostname by entering the prompts. The following script is not working, can anyone of you guide/help me in fixing my script. Response will be appreciated.
--------------------------------------------------------------------------
#!/bin/bash
echo -n "Current host name is :"
hostname

echo -n "Enter the current hostname :"
read current_hostname

if [ $current_hostname == `uname -n` ] ; then
{
echo -n "Enter the new hostname:"
read new_hostname
sed s/$current_hostname/$new_hostname/g >/tmp/hosts_test
sed s/$current_hostname/$new_hostname/g >/tmp/network_test
}
else

echo "You have entered the wrong hostname, Please enter the valid hostname:"

fi


-------------------------------------------------------------------
Since the script is in testing mode, I copied /etc/hosts and /etc/sysconfig/network to /tmp/.
Thanks,
*unix*

Last edited by *unix*; 08-08-2012 at 01:47 AM. Reason: Correcting the script
 
Old 08-08-2012, 03:38 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
Well I am probably not sure about the second echo / read as it seems like a test for the user to read from the screen and then hopefully type in the correct name??

Other than that, are you able to provide more information about:
Quote:
The following script is not working
Are there error messages?
Are the wrong files being changed?

You need to be a lot more specific as a generic 'not working' is very ambiguous.

May I firstly suggest you type each line at the command line and see what happens? (exactly what you have in your script)
 
Old 08-08-2012, 08:21 AM   #3
wigry
Member
 
Registered: Jul 2004
Distribution: slackware
Posts: 196

Rep: Reputation: 49
Instead of trying to fix this particular script with high probability for the future questions I suggest you work through this guide about Bash scripting - answers all your questions and then some

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

For example user input reading is covered in point 10.1

Last edited by wigry; 08-08-2012 at 08:22 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script to change hostname for RHEL wegadnie Linux - General 7 06-22-2011 05:37 AM
bash script ? Change PS1 color on per user basis tommyttt Linux - Software 5 03-01-2010 12:12 AM
Change the Unix prompt to display hostname and home directory kennynoah AIX 3 07-17-2008 06:20 AM
LXer: How to change Linux /UNIX system password-using a web based PHP script user interface LXer Syndicated Linux News 0 08-05-2006 08:33 PM
how to change user in bash script c0d3 Programming 6 07-27-2005 11:34 AM


All times are GMT -5. The time now is 05:20 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration