Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 08-08-2012, 02:27 AM   #1
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.
echo -n "Current host name is :"

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

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


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

Last edited by *unix*; 08-08-2012 at 02:47 AM. Reason: Correcting the script
Old 08-08-2012, 04:38 AM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,988

Rep: Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182
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:
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, 09:21 AM   #3
Registered: Jul 2004
Distribution: slackware
Posts: 225

Rep: Reputation: 53
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

For example user input reading is covered in point 10.1

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


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script to change hostname for RHEL wegadnie Linux - General 7 06-22-2011 06:37 AM
bash script ? Change PS1 color on per user basis tommyttt Linux - Software 5 03-01-2010 01:12 AM
Change the Unix prompt to display hostname and home directory kennynoah AIX 3 07-17-2008 07: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 09:33 PM
how to change user in bash script c0d3 Programming 6 07-27-2005 12:34 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:43 PM.

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