LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   BASH Scripting Troubles with IF and FUNCTION (https://www.linuxquestions.org/questions/linux-software-2/bash-scripting-troubles-with-if-and-function-407630/)

semaja2 01-24-2006 09:55 PM

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


pixellany 01-24-2006 10:19 PM

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

semaja2 01-24-2006 10:31 PM

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

homey 01-24-2006 10:35 PM

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


semaja2 01-24-2006 10:47 PM

well i fixed up all the brackets but still after the archive is made it just skips right to the end of the script

semaja2 01-25-2006 04:03 AM

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

timmeke 01-25-2006 07:46 AM

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

semaja2 01-25-2006 08:24 AM

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


timmeke 01-25-2006 08:51 AM

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.

semaja2 01-25-2006 09:02 AM

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 #


timmeke 01-26-2006 02:04 AM

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.


All times are GMT -5. The time now is 10:30 PM.