LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Idiotically simple bash script problem. (https://www.linuxquestions.org/questions/programming-9/idiotically-simple-bash-script-problem-403125/)

morrolan 01-13-2006 08:04 PM

Idiotically simple bash script problem.
 
Hi,
I'm using a dynamic IP address on one of my machines, but I need SSH access to it so I registered for a dynamic DNS name with dyndns.org.

I was looking through the unofficial ubuntu guide, and they suggest that in order to keep upto date with the DNS records, to write a simple script to update the server.

The script is as follows:

Code:

USERNAME=myusername
PASSWORD=mypassword
HOSTNAME=myhostname.getmyip.org

cd /root/

if [ -f /root/ipcheck.dat ]; then
  ipcheck.py -l -v -r checkip.dyndns.org:8245 $USERNAME $PASSWORD $HOSTNAME
else
  ipcheck.py --makedat -l -v -r checkip.dyndns.org:8245 $USERNAME $PASSWORD $HOSTNAME
fi

I know that the first line doesn't say #!/bin/bash, their script doesn't include it, so I haven't included it either, although I've tried it and it didn't make a difference because I'm calling sh anyway. DynDNS have over 40 TLD's and getmyip.org is one of them. The problem is, I get this when run as root in /root (with all correct permissions):

Code:

me@mymachine:/root$ sudo sh dyndns_update.sh
Password:
: command not foundine 4:
: command not foundine 6:
dyndns_update.sh: line 11: syntax error near unexpected token `fi'
dyndns_update.sh: line 11: `fi'
me@mymachine:/root$

and debugging mode gives:

Code:

me@mymachine:/root$ sudo sh -x dyndns_update.sh
+ USERNAME=$'myusername\r'
+ PASSWORD=$'mypassword\r'
+ HOSTNAME=$'myhostname.getmyip.org\r'
+ $'\r'
: command not foundine 4:
+ cd /root/
+ $'\r'
: command not foundine 6:
dyndns_update.sh: line 11: syntax error near unexpected token `fi'
dyndns_update.sh: line 11: `fi'
me@mymachine:/root$


Could someone please help me? I'm not great with bash but I do know some basic scripting such as what is in my sig, and I've tried allsorts with this but to no avail. I know that the ipcheck.py commands work, and I get the exact same result if I echo these lines.

Many Thanks in advance,

carl0ski 01-13-2006 08:58 PM

Quote:

Originally Posted by morrolan
Hi,
if [ -f /root/ipcheck.dat ]; then

change this to

if [ -f /root/ipcheck.dat ]
then





no ; since it means new command and breaks the
fi at the end

morrolan 01-14-2006 05:39 AM

I've already tried that, and just tried it again, with no difference.

I thought that a semi-colon was just a replacement for a new-line character, allowing you to maximise space and make more sense, by allowing you to execute the commands before the ; and then after. As BASH reads one line at a time and then executes what it found in the line - it doesn't go through executing each command as it finds it.

LasseW 01-14-2006 07:15 AM

It looks as if your script contains CR characters. Get rid of them, eg:

strings oldscript >newscript

carl0ski 01-14-2006 08:00 AM

Quote:

Originally Posted by morrolan
I've already tried that, and just tried it again, with no difference.

I thought that a semi-colon was just a replacement for a new-line character, allowing you to maximise space and make more sense, by allowing you to execute the commands before the ; and then after. As BASH reads one line at a time and then executes what it found in the line - it doesn't go through executing each command as it finds it.

if ls $HOME/fridge/*beer 1>/dev/null 2>&1
then
echo "You have beer! The fridge contains the following:"
for i in $HOME/fridge/*beer
do
head -n 1 $i
done
else
echo "You have no beer! I'll get some."
fi

this seems to work on mine :S

morrolan 01-14-2006 04:22 PM

carl0ski, that isn't what I'm having a problem with, that's my signature!

:p

gilead 01-14-2006 04:46 PM

I think it can't find ipcheck.py - can you put the full path to those python scripts in and see what happens.

morrolan 01-14-2006 05:09 PM

Hi guys, problem solved - in case anyone is interested, there is nothing wrong with the script itself. :scratch:

I moved it into /home/me/dyndns/ and changed the script accordingly, and now it seems to work fine!


All times are GMT -5. The time now is 01:20 AM.