LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash "unexpected end of file" script error (http://www.linuxquestions.org/questions/linux-general-1/bash-unexpected-end-of-file-script-error-556136/)

Runge_Kutta 05-23-2007 01:55 PM

bash "unexpected end of file" script error
 
Hi guys!

I'm getting an "unexpected end of file" script error from the following code below.

I have already tried fixing the "# !/bin/bash" line to "#/bin/bash/," "/bin/ksh," and other variants. I've also tried adding "exit","exit(1)","exit(0)", to the end of the following script. Also, I've made sure bash and ksh are in the /bin/ directory.
Any change that I do simply gives the same following output w/error:
"*Allowing root logins via rlogin and rsh
./root_add2:line 41: syntax error: unexpected end of file"

. . .with "line 41" being the last line of the script file nomatter if the file size of the script ends up being 38 or 42 lines.

Other things you guys might need to know:
I'm running this script on Fedora Core 6 x86_64 system with the intent of migrating the script to a rhel-4-ws blade/node. My intent is to remote login as root account using rlogin command to the rhel-4-ws blade/node from another machine/node - I can already remote login as another user to the rhel-4-ws blade/node using an account other than root.

Code:

# !/bin/bash
# To allow root login via rlogin/rsh, add 'rlogin' and 'rsh' entries
# to /etc/securetty
echo "* Allowing root logins via rlogin and rsh"
if grep '^rlogin' /etc/securetty > /dev/null; then \
  echo "** rlogin is already contained in /etc/securetty"
else \
  cat >> /etc/securetty << EOF
fi #added
rlogin
EOF
#fi - changed!
if grep '^rsh' /etc/securetty > /dev/null; then \
  echo "** rsh is already contained in /etc/securetty"
else \
  cat >> /etc/securetty << EOF
fi #added
rsh
EOF
#fi - changed
cat > /root/.rhosts << EOF
master root
EOF
#
# Enable rsync, rlogin, and rsh
echo "* Enabling rsync, rlogin, and rsh"
chkconfig rsync on
chkconfig rlogin on
chkconfig rsh on
#
# Update /etc/aliases so someone gets root's mail
echo "* Updating /etc/aliases"
if grep '^root:' /etc/aliases > /dev/null; then \
  echo "** root is already contained in /etc/aliases"
else \
  cat >> /etc/aliases << EOF
fi #added
root:  localuser@node01
EOF
exit

. . . the original code looked like the following - part of a kickstart post configuration script
(I just wanted this part of the code to work).

Code:

%post
#/bin/bash
# . . . a bunch of code

# To allow root login via rlogin/rsh, add 'rlogin' and 'rsh' entries
# to /etc/securetty
echo "* Allowing root logins via rlogin and rsh"
if grep '^rlogin' /etc/securetty > /dev/null; then \
  echo "** rlogin is already contained in /etc/securetty"
else \
  cat >> /etc/securetty << EOF
rlogin
EOF
fi
if grep '^rsh' /etc/securetty > /dev/null; then \
  echo "** rsh is already contained in /etc/securetty"
else \
  cat >> /etc/securetty << EOF
rsh
EOF
fi
cat > /root/.rhosts << EOF
master root
EOF
#
# Enable rsync, rlogin, and rsh
echo "* Enabling rsync, rlogin, and rsh"
chkconfig rsync on
chkconfig rlogin on
chkconfig rsh on
#
# Update /etc/aliases so someone gets root's mail
echo "* Updating /etc/aliases"
if grep '^root:' /etc/aliases > /dev/null; then \
  echo "** root is already contained in /etc/aliases"
else \
  cat >> /etc/aliases << EOF
root:  localuser@node01
EOF

the whole kickstart post script looks like this (it's kind of long):

Code:

%post
#/bin/bash
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH
#
# Penguin Cluster Node Assimilation Script
echo "Commencing Penguin Cluster Node Assimilation"
#
# Setup hosts table
echo "* Building /etc/hosts"
cat > /etc/hosts << EOF
127.0.0.1    `hostname` localhost.localdomain localhost
#
192.168.0.1  node01 master
192.168.0.2  node02
192.168.0.3  node03
  .
  .
  .
192.168.0.64  node64
EOF
#
# Configure ipchains for needed service, block everything else
echo "* Reconfiguring the firewall rules"
cat > /etc/sysconfig/ipchains << EOF
:input ACCEPT
:forward ACCEPT
:output ACCEPT
-A input -s 0/0 -d 0/0 22 -p tcp -y -j ACCEPT
-A input -s 0/0 -d 0/0 -i lo -j ACCEPT
-A input -s 0/0 -d 0/0 -i eth0 -j ACCEPT
-A input -s 0/0 -d 0/0 -p tcp -y -j REJECT
-A input -s 0/0 -d 0/0 -p udp -j REJECT
EOF
/etc/rc.d/init.d/ipchains restart
#
# Configure ntp and set the correct time before going any further
echo "* Configuring and starting time service"
cat > /etc/ntp.conf << EOF
restrict default noquery notrust nomodify
restrict 127.0.0.1
restrict 192.168.0.0 mask 255.255.0.0
server 192.168.0.1
driftfile /etc/ntp.drift
logfile /var/log/ntp.log
EOF
cat > /etc/ntp/step-tickers << EOF
192.168.0.1
EOF
chkconfig ntpd on
/etc/rc.d/init.d/ntpd start
#
# Setup /etc/hosts.equiv
echo "* Building /etc/hosts.equiv"
cat > /etc/hosts.equiv << EOF
node01
node02
node03
  .
  .
  .
node64
EOF
#
# Setup NFS mounts
echo "* Establishing NFS mounts"
mkdir -p /home
mkdir -p /u1
if test -f /etc/fstab.assim-save; then \
  echo "** WARNING: Modifying previously-saved fstab file instead of the current one"
else \
  cp -p /etc/fstab /etc/fstab.assim-save
fi
cat > /etc/fstab << EOF
`cat /etc/fstab.assim-save`
node01:/home  /home  nfs    soft,bg,intr  0 0
node01:/u1    /u1    nfs    soft,bg,intr  0 0
EOF
mount -at nfs
#
# Configure a serial console
echo "* Configuring for serial console"
if grep '^co:' /etc/inittab > /dev/null; then \
  echo "** Serial console already configured"
else \
  cat >> /etc/inittab << EOF
# Serial console since this machine has no head
co:2345:respawn:/sbin/agetty ttyS0 9600 vt100
EOF
fi
if grep '^ttyS0' /etc/securetty > /dev/null; then \
  echo "** ttyS0 is already contained in /etc/securetty";
else \
  cat >> /etc/securetty << EOF
ttyS0
EOF
fi
#
# Update /etc/hosts.allow and /etc/hosts.deny
echo "* Setting up /etc/hosts.allow and /etc/hosts.deny"
cat > /etc/hosts.allow << EOF
#
# hosts.allow  This file describes the names of the hosts which are
#              allowed to use the local INET services, as decided
#              by the '/usr/sbin/tcpd' server.
#

ALL: 192.168.0.
EOF
cat >> /etc/hosts.deny << EOF
#
# hosts.deny  This file describes the names of the hosts which
#              are *not* allowed to use the local INET services,
#              as decided by the '/usr/sbin/tcpd' server.
#
# The portmap line is redundant, but it is left to remind you that
# the new secure portmap uses hosts.deny and hosts.allow. In particular
# you should know that NFS uses portmap!

ALL: ALL
EOF
#
# To allow root login via rlogin/rsh, add 'rlogin' and 'rsh' entries
# to /etc/securetty
echo "* Allowing root logins via rlogin and rsh"
if grep '^rlogin' /etc/securetty > /dev/null; then \
  echo "** rlogin is already contained in /etc/securetty"
else \
  cat >> /etc/securetty << EOF
rlogin
EOF
fi
if grep '^rsh' /etc/securetty > /dev/null; then \
  echo "** rsh is already contained in /etc/securetty"
else \
  cat >> /etc/securetty << EOF
rsh
EOF
fi
cat > /root/.rhosts << EOF
master root
EOF
#
# Enable rsync, rlogin, and rsh
echo "* Enabling rsync, rlogin, and rsh"
chkconfig rsync on
chkconfig rlogin on
chkconfig rsh on
#
# Update /etc/aliases so someone gets root's mail
echo "* Updating /etc/aliases"
if grep '^root:' /etc/aliases > /dev/null; then \
  echo "** root is already contained in /etc/aliases"
else \
  cat >> /etc/aliases << EOF
root:  localuser@node01
EOF
newaliases
fi
#
# useradd may be used to establish user accounts on the compute node.
# Alternatively, the master node may provide user/group databases using rsync
# after the installation process is complete.
#
# Install my favorite analysis package
rpm -i /u1/packages/analpack-1.0-5.i386.rpm
# Install my favorite commercial compiler
tar xzpf /u1/packages/xyz-f90.tar.gz
#
echo "Penguin Cluster Node Assimilation Complete"
exit

sorry for the long post, just wanted to make sure everyone had enough info to sink their teeth into this problem. I need fresh eyes to go over this script ( probably some dumb syntactical error, as this is my first try at scripting under linux) while I'm out for lunch :-)

raskin 05-23-2007 03:00 PM

Your
Code:

fi #added
will go to file, not to interpreter. And the last 'else' is not closed because of this.

By the way, you do not want to add rlogin to securetty. Rlogin goes entirely unencrypted. So it is way too easy to intercept and then abuse. And logging in as root by rlogin means actively seeking troubles. Root login console should be acquired - if needed remotely - by a 'su -' from an ssh session.. Notice that original script marked ttyS0 as secure - it is COM port that requires physical access to the back side of computer. Not just physical access to any wire going to the same switch as the box you'll use to login from...

lakris 05-23-2007 03:06 PM

The problem lies in the method of representing linefeed/carriage return in text files between different operating systems. Try to use the utility
dos2unix
on the file on Your Linux-machine to fix this. I guess You created this on a Windows-machine and copied it to the Linux-machine?

Just my few cents.
/L

Runge_Kutta 05-23-2007 03:12 PM

yea, about that login as root thing (I forgot to mention it as I knew someone would bring it up and I didn't want anyone wasting any brain power about it):

I just walked on the job two weeks ago - I know that logging in as root is bad (I'm close to a reformat on another blade/node because of this - people are abusing the system): the server was configured this way before I got here and that is how people have been using/logging into the server nodes. I'll have a chance to reconfigure the server login process later; but for now, they (clients) want it to work this way - btw, the server is connected (at least, supposed to be) to an 'intranet' switch which is not connected to the internet/outside world in anyway (and only about ten people use this server system for intensive calculation purposes).

So yea, if anyone knows of an easier way to accomplish this (remote login as root to rhel-4-ws system) then please let me know - otherwise, help me in fixing above script.

Quote:

Originally Posted by lakris
The problem lies in the method of representing linefeed/carriage return in text files between different operating systems. Try to use the utility
dos2unix
on the file on Your Linux-machine to fix this. I guess You created this on a Windows-machine and copied it to the Linux-machine?

Just my few cents.
/L

the script comes from a kickstart post script used to configure some settings after an installation of Linux (i think rhel), so no, above script does not originate in dos (see last code segment in 1st post).

-----------------------------------------------------------------------------------------
ignore my previous statement lakris as I just realized what you said/meant. I'll try both your windows2unix method and what raskin pointed out as lines of code that I carelessly am sending to file (I think I need to move the "fi" statements after "EOF"? - Ok, I go try now . . .
-----------------------------------------------------------------------------------------
Update:

I tried the above and I still get the error. Script, now, looks like below:

Code:

# !/bin/bash
# To allow root login via rlogin/rsh, add 'rlogin' and 'rsh' entries
# to /etc/securetty
echo "* Allowing root logins via rlogin and rsh"
if grep '^rlogin' /etc/securetty > /dev/null; then \
  echo "** rlogin is already contained in /etc/securetty"
else \
  cat >> /etc/securetty << EOF
rlogin
EOF
fi# - changed back!
if grep '^rsh' /etc/securetty > /dev/null; then \
  echo "** rsh is already contained in /etc/securetty"
else \
  cat >> /etc/securetty << EOF
rsh
EOF
fi# - changed back!
cat > /root/.rhosts << EOF
master root
EOF
#
# Enable rsync, rlogin, and rsh
echo "* Enabling rsync, rlogin, and rsh"
chkconfig rsync on
chkconfig rlogin on
chkconfig rsh on
#
# Update /etc/aliases so someone gets root's mail
echo "* Updating /etc/aliases"
if grep '^root:' /etc/aliases > /dev/null; then \
  echo "** root is already contained in /etc/aliases"
else \
  cat >> /etc/aliases << EOF
root:  localuser@node01
EOF
fi #Added


lakris 05-23-2007 04:01 PM

-----------------------------------------------------------------------------------------
ignore my previous statement lakris as I just realized what you said/meant. I'll try both your windows2unix method and what raskin pointed out as lines of code that I carelessly am sending to file (I think I need to move the "fi" statements after "EOF"? - Ok, I go try now . . .[/QUOTE]
***************************
Yeah, and sorry, I was too hot on the kbd as well, there are actual syntax errors in your script. What i hinted at usually manifest itself in "command not found" error.
The first cat statement actually appends "fi #added" to the file /etc/securetty, which probably isn't what You wanted and as Raskin said, leaves the if-then-else incomplete. It is important where You put the delimiters, if-then-else-fi. And the magic that EVERYTHING between the two EOF (and the second one must start a line) is regarded as one string of bytes/characters.
You should do something like (for this particular piece of code)
<code>
#!/bin/bash
# To allow root login via rlogin/rsh, add 'rlogin' and 'rsh' entries
# to /etc/securetty
echo "* Allowing root logins via rlogin and rsh"
if grep '^rlogin' /etc/securetty > /dev/null; then # Searches for a line starting with rlogin
echo "** rlogin is already contained in /etc/securetty"
else
#Next command echoes everything between the two EOF to the file /etc/securetty
cat >> /etc/securetty << EOF
rlogin
EOF
fi
</code>
etc...

But, I feel it would be cleaner to do
<code>
if grep '^rlogin' /etc/securetty > /dev/null; then
echo "** rlogin is already contained in /etc/securetty"
else
echo rlogin >> /etc/securetty
fi
</code>
More obvious in a way. EOF is usually used when You have A LOT of text to output. Using >> (append) is important, so You don't use > (output) which will overwrite the file.

Runge_Kutta 05-23-2007 04:18 PM

Success!!!
 
Everything now works, and I was able to run the script on the target machine/node and now users are able to log in easily as root (even though it's still a bad idea :tisk: - but hey, that's what they want :D )

apparently,one of the other changes I needed in the script was a space between the "fi" statement and the comment "# - changed back"

raskin - thanks!
lakris! welcome to LQ.org!


Thanks guys for all the help, and the final product came out looking like this:

Code:

#!/bin/ksh
# To allow root login via rlogin/rsh, add 'rlogin' and 'rsh' entries
# to /etc/securetty
echo "* Allowing root logins via rlogin and rsh"
if grep '^rlogin' /etc/securetty > /dev/null; then \
  echo "** rlogin is already contained in /etc/securetty"
else \
  cat >> /etc/securetty << EOF
rlogin
EOF
fi # - changed back!
if grep '^rsh' /etc/securetty > /dev/null; then \
  echo "** rsh is already contained in /etc/securetty"
else \
  cat >> /etc/securetty << EOF
rsh
EOF
fi # - changed back!
cat > /root/.rhosts << EOF
master root
EOF
#
# Enable rsync, rlogin, and rsh
echo "* Enabling rsync, rlogin, and rsh"
chkconfig rsync on
chkconfig rlogin on
chkconfig rsh on
#
# Update /etc/aliases so someone gets root's mail
echo "* Updating /etc/aliases"
if grep '^root:' /etc/aliases > /dev/null; then \
  echo "** root is already contained in /etc/aliases"
else \
  cat >> /etc/aliases << EOF
root:  localuser@node01
EOF
fi #Added


lakris 05-23-2007 04:36 PM

Nice!
The fi# was the culprit, I didn't realize. :)


All times are GMT -5. The time now is 11:37 PM.