LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-24-2006, 09:55 PM   #1
semaja2
LQ Newbie
 
Registered: Jan 2006
Posts: 14

Rep: Reputation: 0
BASH Scripting Troubles with IF and FUNCTION


Hey guys, im running macosx 10.4.4 which of course uses linux under neath and ive been trying to write up a script for both my mac and linux box, but at the momment it seems the script hits a halt during the archive bit

Code:
#! /bin/bash

#----Backup Details------------------------#
BACKUPNAME="xxxxx"
BACKUPDIR="~/"
NAMET=~/$BACKUPNAME-$(date +%d%m%Y).tar.gz
NAMEE=~/$BACKUPNAME-$(date +%d%m%Y).aes
REMOVETEMP=1
#----Online Backup details-----------------#
ONLINE=1
FTPUSER=xxxxx
FTPPASS=xxxxx
FTPDIR=xxxxxx
FTPSITE=xxxxxx
#----Server Backup Details-----------------#
SERVER=1
BACKUPDRIVE=xxxxx
SHARE=xxxxx
SMBSERVER=xxxxxx
#----Encryption Details--------------------#
ENCRYPTBACKUP=1
ENCRYPTPASS=xxxxxxx


function BACKUPFTP {
growlnotify -m "Uploading backup to FTP" -t $BACKUPNAME
echo "uploading to ftp"
if [ ENCRYPTBACKUP = 1]; then
ftp -n <<EOF
open $FTPUSER $FTPSITE
user $FTPPASS
put $NAMEE $FTPDIR
bye
EOF
elif [ENCRYPTBACKUP = 0]; then
ftp -n <<EOF
open $FTPUSER $FTPSITE
user $FTPPASS
put $NAMET $FTPDIR
bye
EOF
fi
}

function REMOVETEMP {
#removes archive
echo "removing archives"
growlnotify -m "Removing Files used" -t $BACKUPNAME
rm $NAMET
rm $NAMEE
}

function ENCRYPT {
#Encrypts the archive
echo "encrypting files"
growlnotify -m "Encrypting backup" -t $BACKUPNAME
openssl enc -e -a -salt -aes-256-cbc -in $NAMET -out $NAMEE -k $ENCRYPTPASS
}

function MOUNTSHARE {
#mounts the backup drive
echo "Mounting backupdrive"
growlnotify -m "Mounting Backup Drive" -t $BACKUPNAME
mount_smbfs -I $SMBSERVER $SHARE $BACKUPDRIVE
}
#unmounts the backupdrive

function UNMOUNTSERVER {	
echo "unmounting backupdrive"
growlnotify -m "Unmounting backupdrive" -t $BACKUPNAME
umount $BACKUPDIR
}
function UPLOADSERVER {
echo "Uploading archive to server"
growlnotify -m "Copying to Backup Drive" -t $BACKUPNAME
if [ BACKUPDRIVE = 1]; then
cp $NAMEE $BACKUPDRIVE
elif [BACKUPDRIVE = 0]; then
cp $NAMET $BACKUPDRIVE
fi
}


#Archives the files
function ARCHIVE {
echo "archiving files"
growlnotify -m "Archiving backup" -t $BACKUPNAME
tar -czf $NAMET $BACKUPDIR
}


echo "Annoucing Backup"
growlnotify -m "Backup Started" -t $BACKUPNAME -s

ARCHIVE

if [ "ENCRYPTBACKUP" = "1" ]; then
ENCRYPT
fi

if [ "ONLINE" = "1" ]; then
BACKUPFTP
fi

if [ "SERVER" = "1" ]; then
MOUNTSHARE
UPLOADSERVER
UNMOUNTSERVER
fi

if [ "CLEANUP" = "1" ]; then
REMOVETEMP
fi

growlnotify -m "Backup COMPLETED" -t $BACKUPNAME -s

Last edited by semaja2; 01-24-2006 at 09:59 PM.
 
Old 01-24-2006, 10:19 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
Quote:
Originally Posted by semaja2
Hey guys, im running macosx 10.4.4 which of course uses linux under neath
No....it is not Linux. I believe that OS-X derives from BSD Unix
 
Old 01-24-2006, 10:31 PM   #3
semaja2
LQ Newbie
 
Registered: Jan 2006
Posts: 14

Original Poster
Rep: Reputation: 0
well its not exactly linux but much in the terminal is near the same cmds as a linux machine, anywho the fact is this is about bash scripting which both have, so can someone help me and tell me if anything is wrong with it
 
Old 01-24-2006, 10:35 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 60
I don't know how it is on your box but, on my FC4 machine, the if statement doesn't work when the [ brackets ] are too close to the [number].

Code:
#!/bin/bash

num=1

if [ $num = 1 ] ; then
echo good
else
echo not good
fi
 
Old 01-24-2006, 10:47 PM   #5
semaja2
LQ Newbie
 
Registered: Jan 2006
Posts: 14

Original Poster
Rep: Reputation: 0
well i fixed up all the brackets but still after the archive is made it just skips right to the end of the script
 
Old 01-25-2006, 04:03 AM   #6
semaja2
LQ Newbie
 
Registered: Jan 2006
Posts: 14

Original Poster
Rep: Reputation: 0
also is there a way to make it link to another file, so like in c++ u could have another file with ur functions in it is this possible in bash
 
Old 01-25-2006, 07:46 AM   #7
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Read the man pages. I suppose there is a possibility to include other files (like the #include in C).
For instance, in the /etc/init.d/* scripts, often written in Bash, the script /etc/init.d/functions is usually "included" by:
. ./etc/init.d/functions

Also, you should pay attention to programming syntax. Several shells have different if-syntax for numeric comparisons (called "ARITHMETIC EVALUATION") and for string/other comparisons. So, if your variable stores a number, you should use the number syntax, not the string syntax, etc.
If you want to test if a number equals 1 in Bash, I think it's supposed to be written as:
if (( $num == 1 )); then
do_some_code;
fi;
But, to test if a file exists, you need to use:
if [[ -f $file_name ]]; then
do_some_code;
fi;

In short, read "man bash", like the rest of us - lol
 
Old 01-25-2006, 08:24 AM   #8
semaja2
LQ Newbie
 
Registered: Jan 2006
Posts: 14

Original Poster
Rep: Reputation: 0
Bad Interpreter

Hey guys, this one is a new question but ill stay in one thread, im trying to build a few scripts for my suse 10 box but when ever i try to run the script i get this

bender:/home/gserver # ./utilities.sh
: bad interpreter: No such file or directory
Code:
#!/bin/bash


UTILITES="CSMAPLIST REBOOT SHUTDOWN"
select utl in $UTILITES; do

     if [ "$utl" = "CSMAPLIST" ]; then
     
     echo "Listing Counter Strike maps.."
     cat | ls /home/gserver/games/csmaps
     
     elif [ "$utl" = "REBOOT" ]; then
     
     sudo init 6
     
     elif [ "$utl" = "SHUTDOWN" ]; then
     
     sudo shutdown now
     
     else
     echo
     echo
     echo ERROR : BAD OPTION, TRY AGAIN
     fi
    
done
 
Old 01-25-2006, 08:51 AM   #9
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Basically, #!/bin/bash tells your system that it should use the bash shell (the program /bin/bash) to interprete the code below that line.
In other words, your script uses the Bash shell syntax.

However the error "bad interpreter" suggests that you don't have the /bin/bash program.

Try doing
ls -l /bin/bash
whereis bash
If they don't tell you that you have bash, you probably don't have it. /bin/sh is similar, but somewhat older and thus may lack some features of Bash. Try changing #!/bin/bash into #!/bin/sh and run the script again to see if it works.

Otherwise,
echo $SHELL
to print the shell you're currently using. You could try re-writing your script using that shell (and it's specific syntax) as well.
 
Old 01-25-2006, 09:02 AM   #10
semaja2
LQ Newbie
 
Registered: Jan 2006
Posts: 14

Original Poster
Rep: Reputation: 0
EDIT: ok my mistake it seems that my windows notepad2 i had been using had placed werid formatting to it even tho its txt, is there any good notepad apps that do the colouring of text for certain functions

PS. i know about vim but thats just all one colour



im sure bash is there and working becuase i have other scripts that load and run fine, i copied that direct line from another script that i know works.

also here is the out put of the cmd

Code:
bender:/home/gserver # ls -l /bin/bash
-rwxr-xr-x  1 root root 490716 Sep 10 01:42 /bin/bash
bender:/home/gserver #

Last edited by semaja2; 01-25-2006 at 09:05 AM.
 
Old 01-26-2006, 02:04 AM   #11
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
On Windows, you could try using a professional editor rather than notepad or wordpad (that won't do syntax highlighting).
Try something like TextPad or UltraEdit. You can try them for free, but have to pay for a license.

As for vim, it shouldn't put everything in just one colour. My vim recognizes my scripts and source code just fine and highlights the syntax correctly. If that's not the case for you, I'd recommend you taking a look at your syntax. Maybe you've accidentally commented out everything or disabled syntax highlighting?

If you want other editors then vim, try using a graphical one, like gedit, nedit, kedit, etc.
Search this forum for others.
 
  


Reply


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
Shell Scripting Troubles!!! sc_3007 Linux - General 3 11-30-2005 01:21 PM
Shell scripting: how to load a function from a file? frankie_DJ Programming 2 08-16-2005 02:16 PM
Bash Scripting - Using a function as test. ldp Programming 1 01-04-2005 06:31 AM
BASH If-then-else Scripting Help xianzai Programming 4 10-29-2004 04:09 AM
Mail Function in bash scripting chynna_v Programming 2 09-14-2004 04:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:49 AM.

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