LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   New Unix/Linux user - trying to a write a bash script to change the hostname on RHEL (https://www.linuxquestions.org/questions/linux-newbie-8/new-unix-linux-user-trying-to-a-write-a-bash-script-to-change-the-hostname-on-rhel-4175420945/)

*unix* 08-08-2012 01:27 AM

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*

grail 08-08-2012 03:38 AM

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)

wigry 08-08-2012 08:21 AM

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


All times are GMT -5. The time now is 08:35 PM.