+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
*nix Thread, Will I ever get to grips with linux? Running a script .sh in Technical; Ok thanks to webman and others have my Zimbra installation up and running. I can run a number of commands ...
  1. #1
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29

    Will I ever get to grips with linux? Running a script .sh

    Ok thanks to webman and others have my Zimbra installation up and running. I can run a number of commands to backup the aforementioned zimbra but I can't save those commands as a script .sh and then run that?

    I have saved my script as zbackup.sh

    I then tried runnig it but no go.
    I have a read round and find I need to chmod it.

    I tried chmod it like so

    $ chmod ugo+rx zbackup.sh

    but I still get this error when I type "zbackup.sh" at the prompt

    -bash: zbackup.sh: command not found

    Am I missing an abvious, to linux, step?

    Thanks

  2. #2

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,529
    Thank Post
    513
    Thanked 2,406 Times in 1,862 Posts
    Blog Entries
    24
    Rep Power
    822
    Couple of things. Is the file in the same directory you are trying to run the command from? ie is it in your /home/user and your console at that location too?

    Next, is the file secretly named 'zbackup.sh.txt' or something?

  3. #3

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,400
    Thank Post
    636
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    319
    It's a security feature. As you have already applied the execute permission (chmod +x) you can run the script like this:

    Code:
    ./zbackup.sh
    If it doesn't have the execute permission, you need to run it as a parameter:
    Code:
    sh zbackup.sh

  4. Thanks to webman from:

    reggiep (26th February 2009)

  5. #4
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    the sh zbackup.sh worked, well it started the script anyway!

    Thanks

  6. #5

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,529
    Thank Post
    513
    Thanked 2,406 Times in 1,862 Posts
    Blog Entries
    24
    Rep Power
    822
    Doh! How didn't I spot that?!

  7. #6
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    Can I come back in with my next problem now?

    I have my script, well webmans with some different paths, and I can execute it by typing "sh zbackup.sh" at the prompt BUT when I do execute it I get a response as follows...


    : command not found
    : command not found
    : command not found
    : command not found:
    : command not found:
    : command not found:
    : command not found:
    : command not found:
    : command not found:
    'backup.sh: line 27: syntax error near unexpected token `{
    'backup.sh: line 27: `function out {

    I'm not sure how I have managed to cock up since it works fine if I paste the text straight in to the prompt!

    Code:
    #!/bin/bash
    
    #
    # Zimbra Backup Script
    #
    
    
    # Variables
    TIME=`date +%Y-%m-%d`
    DOWN=`date +%u`
    DOWD=`date +%A`
    
    SOURCE="/opt/zimbra/"
    DEST="/tmp/zcsbackup"
    
    NFSHOST="10.60.28.53"
    NFSPATH="/Volumes/Promise/zimbra"
    NFSMOUNT="/mnt/mac-1"
    
    ARCHIVENAME="zcsbackup-$DOWN-$DOWD.tar.gz"
    ARCHIVE="/tmp/$ARCHIVENAME"
    
    LOG="/var/log/zmbackup.log"
    EMAIL="ict@example.com"
    
    
    function out {
    	echo "["`date +"%Y-%m-%d %T"`"] $1"
    }
    
    
    #echo ""
    #echo "* * * * * * * * * *"
    #echo "Zimbra backup"
    #echo `date +%Y-%m-%d`
    #echo ""
    
    
    echo ""
    out "Zimbra backup"
    echo ""
    
    
    echo ""
    out "Stopping services"
    echo ""
    
    
    # Stop Zimbra Services
    su - zimbra -c /opt/zimbra/bin/zmcontrol stop
    sleep 5
    
    
    echo ""
    out "Zimbra status:"
    echo ""
    su - zimbra -c /opt/zimbra/bin/zmcontrol status
    sleep 10
    
    
    echo ""
    out "Making directory and executing Rsync"
    echo ""
    
    # Make directory and sync to it
    mkdir $DEST
    rsync -aHK --exclude=*.pid $SOURCE $DEST
    
    
    echo ""
    out "Restarting Zimbra services"
    echo ""
    
    # Restart Zimbra Services
    su - zimbra -c /opt/zimbra/bin/zmcontrol start
    sleep 5
    
    
    echo ""
    out "Zimbra status:"
    echo ""
    su - zimbra -c /opt/zimbra/bin/zmcontrol status
    
    
    echo ""
    out "Creating archive"
    echo ""
    
    # Create archive of backed-up directory for transfer
    tar -zcf $ARCHIVE -C $DEST .
    
    
    echo ""
    out "Copying to NFS server"
    echo ""
    
    mkdir $NFSMOUNT
    mount -t nfs $NFSHOST:/$NFSPATH $NFSMOUNT
    cp -fv $ARCHIVE $NFSMOUNT/
    ls -lah $NFSMOUNT
    umount $NFSMOUNT
    rm -rfv $NFSMOUNT
    
    
    echo ""
    out "Removing $DEST and $ARCHIVE from local disk"
    echo ""
    
    # Remove temp backup destination, samba and archive
    rm -rf $DEST
    rm -fv $ARCHIVE
    
    echo ""
    out "Backup complete!"
    echo ""
    echo "----------"
    
    
    cat $LOG | mail -c '' -s "[Zimbra backup] `hostname --fqdn` $TIME" $EMAIL

  8. #7

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,400
    Thank Post
    636
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    319
    Ah. Instead of sh, try bash. My mistake

  9. #8

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    For background, the first line of the script (#!/bin/bash) tells you which interpreter should be used to run it.

    It's in that weird format so that the shell can also tell, so if you just call ./zbackup.sh instead of bash zbackup.sh, things work as expected. Being able to do this is more convenient, since you don't have to dig into the script to find out what interpreter should be using it.

    By convention, if you have that first line in a script (it's called a 'shebang' or 'hashbang') and the file has the execute bit on, you usually don't put a suffix on the script name. So yours would just be called zbackup. This makes it obvious that the file has a shebang and can be executed directly.
    Last edited by powdarrmonkey; 26th February 2009 at 10:02 AM.

  10. Thanks to powdarrmonkey from:

    reggiep (26th February 2009)

  11. #9
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    with either sh or bash I still get the error I mentioned before?

    On a separate note thanks to everyone who has helped me with this so far. I would be nowhere without your help.
    I'm enjoying this long and winding road which is linux.

  12. #10

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,400
    Thank Post
    636
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    319
    Have you tried changing to the directory the script is in and running it using the ./ method? As long as it has the +x attribute it should work as powdarrmonkey said.

  13. #11
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    Yep in in the directory that the zbackup.sh is in. If I type ./zbackup.sh I get a different error!

    -bash: ./zbackup.sh: /bin/bash^M: bad interpreter: No such file or directory


    Does that mean anything?

  14. #12

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    Yes. Say "which bash" to check where bash is living, and set your shebang to there instead.

  15. #13

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,802
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224
    The file is in DOS CR/LF (the ^M is the CR character) rather than UNIX LF style. Whatever you used to transfer the file from your windows PC mangled it. I use WinSCP here and it will detect and do the right thing(tm).

    Use 'dos2unix <filename>' to fix it.

    You might want to try editing/creating scripts directly on your Linux machine in future. I do it via a SSH session using 'nano'. If you use Putty as your SSH client you can right click to mass paste text into the editor.

  16. 2 Thanks to Geoff:

    powdarrmonkey (26th February 2009), reggiep (26th February 2009)

  17. #14

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,644
    Thank Post
    858
    Thanked 645 Times in 428 Posts
    Rep Power
    498
    Damn you Geoff - was jus about to say that the ^M didn't look right..

  18. #15

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    Ah, good spot. I wondered, then forgot.

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Script to check if app is running ...
    By pooley in forum Windows
    Replies: 4
    Last Post: 5th January 2009, 11:31 AM
  2. kix script not running all the time
    By kubasa in forum Windows
    Replies: 11
    Last Post: 4th March 2008, 10:29 PM
  3. Printer script not running at start up
    By richard in forum Scripts
    Replies: 11
    Last Post: 29th May 2007, 07:39 PM
  4. Need help getting to grips with subnetting
    By Kyle in forum Wireless Networks
    Replies: 26
    Last Post: 3rd April 2007, 11:57 AM
  5. Replies: 1
    Last Post: 31st January 2006, 03:51 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •