Find the answer to your Linux question:
Results 1 to 3 of 3
I have this backup script that works well, #/bin/bash finished="false" while [ "$finished" = "false" ] do clear #chose the path to be backedup echo "Enter the path of the ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    9

    Backup Script

    I have this backup script that works well,
    #/bin/bash

    finished="false"

    while [ "$finished" = "false" ]
    do
    clear

    #chose the path to be backedup

    echo "Enter the path of the directory to be backed up"

    read backup_location

    #chose the path of the destination

    echo "Enter the path of where you want the backup saved"

    read destination_location



    #informing us if the backup was successful or not
    tar -cvf $destination_location $backup_location
    code=$?
    if [ $code -eq 0 ]
    then
    echo "Backup successfull"
    else
    echo "Backup did not work press any key to run again or n to exit"
    fi



    #the loop to either exit or restart the program



    read resp

    if [ "$resp" = "n" ]
    then
    finished="true"
    fi
    done

    echo "exiting backup.sh"

    But i need to modify it so that to run it you just have to type,
    ./backup.sh <directory to be backed up> <directory to save to>
    then the script automaticly does evereythong so i do not have to propmt users once the command ahs been entered but i have no idea how to split the two variables adn take them from after the initial command and use them in the script.

    How can i split the variables and get teh script to run with one simple command

    thanks for your help i am new to scripting

  2. #2
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    Hi and Welcome to the LinuxForums !

    You can use $#, $*, $1, $2, $3.... etc to access parameters passed with script name.
    Here is an example:
    Code:
    #!/bin/bash
    
    # Its a test script : test.sh
    
    echo "First Parameter : \$1 = $1"
    echo "Second parameter : \$2 = $2"
    echo "Total passed parameters : \$# = $#"
    echo " All parameters : \$* = $*"
    exit 0
    Save it as test.sh and execute this
    Code:
    chmod +x test.sh
    ./test.sh This is a test script
    This is a test script are parameters passed to script test.sh.
    Check linuxcommand.org too.

    HTH !
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    9
    Thanks a lot it has helped heaps

Posting Permissions

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