0

Learning stage of understanding script. I have to make these below change on every new system. So I created these script, unfortunately it isn't working.

I want to understand that if the user input is read and kept in a variable how can I use it again after some time. Like in this script I have asked the user for their input Is this a DNS Server and what is the rev of the server.

#!/bin/bash


    echo -n "Is this a DNS Server [y n]?"
    read command

    if [ $command = n ]
            then
                    yum -y install dnsmasq
                    yum -y install net-snmp net-snmp-utils
    elif [ $command = n ]
            then
                    echo $command

    else
            echo "DNS Package installation should be excluded"

    fi

cat <<EOF>>  scriptos.sh
!/bin/sh


export rev="avi"
export DNS_FLG="Y"
export DNS_FLG="N"
EOF


echo -n "what is the rev of the server"
read rev

if [ $rev = y ]
        then
                echo export LOC=$rev
if [ $rev = N ]
        then
                echo export DNS_FLG="Y"

if [ $rev = Y ]
        then
                echo export DNS_FLG="Y"

fi


echo "what your GW"
read GW
echo "what is your NW"
read NW

echo 192.168.0.0/16 via ${GW}  >  /etc/sysconfig/network-scripts/route-eth1
echo ${NW} via ${GW} >> /etc/sysconfig/network-scripts/route-eth1


/etc/init.d/network restart

This script is not working because of this bellow error.

[root@centos6 ~]# ./script
Is this a DNS Server [y n]?y
DNS Package installation should be excluded
what is the rev of the servery
./script: line 57: syntax error: unexpected end of file
1
  • The first errors are caused by the malformatted expression in the if-statement. Please add your n inside quotes. (The $command can also be quoted in case there might be a whitespace).
    – AirPett
    May 26, 2016 at 7:13

1 Answer 1

1

First: Programming languages are picky about syntax. In sh/bash, the [ works as a standalone command (unlike parens in most other languages), so it needs to be separated by spaces, as well as all its arguments. Therefore:

if [ "$command" = y ]; then
    …
elif [ "$command" = n ]; then
    …
fi

Second: Many of your condition blocks are missing the closing fi. It's always if…then…fi.

Third: Some of your prompts check for lowercase y/n, others check for uppercase Y/N. You should consistently accept the same inputs everywhere. For example:

# option 1 – make the variable lower-case

if [ "${command,,}" = y ]; then
    …

# option 2 (bash-only) – use extended match

if [[ $command == @(y|Y) ]]; then
    …

# option 3 (sh/bash) – use 'case' alternatives

case $command in
    y|Y)
        … ;;
    n|N)
        … ;;
esac

Fourth: <<EOF redirects input. The echo command does not take any input (only command line). You would want to use cat <<EOF instead, and don't forget to finish the text with an EOF line.

Finally, make sure to put the #!/bin/sh or #!/usr/bin/env bash header at the top of all your scripts.

3
  • Grawity I have modified the question as per you suggestion. But still its getting error on line 57.
    – AReddy
    May 26, 2016 at 9:32
  • I am able to see the file created scriptos.sh its asking me echo -n "what is the rev of the server" but when I enter the any thing it gives me error ./script: line 57: syntax error: unexpected end of file
    – AReddy
    May 26, 2016 at 9:37
  • See points #1 and #2; you haven't fixed those in the 2nd half yet. May 26, 2016 at 11:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .