Find the answer to your Linux question:
Results 1 to 9 of 9
Hi guys I'm trying my hand at arrays in bash for a backup script. Now I not sure if this is the correct thing to do and just look at ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    21

    Post Using an array in bash for mutiple paths


    Hi guys I'm trying my hand at arrays in bash for a backup script. Now I not sure if this is the correct thing to do and just look at website and amended but does'nt work. Could someone tell me where I'm going wrong

    Thanks in advanced

    LOG_DIR="/home/ops/Desktop/temp"
    array[0]=3
    array[1]=/home/ops/Desktop/dir1
    array[2]=/home/ops/Desktop/dir2
    array[3]=/etc/dir3

    cd $LOG_DIR
    tar cvzf backup-"$(date +%d-%b-%y)".tar $array

    this is the error

    LOG_DIR="/home/ops/Desktop/temp"
    array[0]=3
    array[1]=/home/ops/Desktop/dir1
    array[2]=/home/ops/Desktop/dir2
    array[3]=/etc/dir3

    This is the error I get
    tar: 3: Cannot stat: No such file or directory

  2. #2
    Linux Newbie Nagarjuna's Avatar
    Join Date
    Feb 2011
    Posts
    122
    When running the tar command, be sure you reference the array like so:

    Code:
    tar czvf backup-"$(date +%d-%b-%y)".tar ${array[@]}
    Writing out the array variable like above tells tar to run against each individual entry in the array.

    Also, what is the purpose of 'array[0]=3'? Does a file named '3' exist? This may cause some problems with tar.

    I recreated the script on my computer and it works. Here is the code I used:

    Code:
    #!/bin/bash
    
    array[0]=/home/tristan/scripts/access
    array[1]=/home/tristan/scripts/backup
    array[2]=/home/tristan/scripts/newuser
    array[3]=/home/tristan/scripts/pacman
    
    echo ${array[@]}
    tar czvf backup-"$(date +%d-%b-%y)".tar ${array[@]}
    I hope this helps. If not, let me know and I'll try to assist further.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    21
    Quote Originally Posted by Nagarjuna View Post
    When running the tar command, be sure you reference the array like so:

    Code:
    tar czvf backup-"$(date +%d-%b-%y)".tar ${array[@]}
    Writing out the array variable like above tells tar to run against each individual entry in the array.

    Also, what is the purpose of 'array[0]=3'? Does a file named '3' exist? This may cause some problems with tar.

    I recreated the script on my computer and it works. Here is the code I used:

    Code:
    #!/bin/bash
    
    array[0]=/home/tristan/scripts/access
    array[1]=/home/tristan/scripts/backup
    array[2]=/home/tristan/scripts/newuser
    array[3]=/home/tristan/scripts/pacman
    
    echo ${array[@]}
    tar czvf backup-"$(date +%d-%b-%y)".tar ${array[@]}
    I hope this helps. If not, let me know and I'll try to assist further.

    Thank you for help that has work great

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User stokes's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    274
    I've been a Linux user for quite a few years now, but have never seen this kind of syntax used before. Usually the following more simple syntax is used to with with arrays in shell scripts.

    Code:
    #!/bin/bash
    
    array="~tristan/scripts/access ~tristan/scripts/backup ~tristan/scripts/newuser ~tristan/scripts/pacman"
    
    echo ${array}
    tar czvf backup-$(date +%d-%b-%y).tar ${array}
    Registered Linux user #389109
    My Semi-Linux Blog

  6. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,303
    Hi.

    I infer that stokes is saying that you don't need arrays in this instance. Here's a short script that shows how bash might handle this:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate defining and dereferencing arrays in shell script.
    
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && . $C
    
    array=( x y z )
    scalar="x y z"
    
    pl " Contents of entire array:"
    echo ${array}
    echo ${array[*]}
    echo ${array[@]}
    
    pl " Contents of scalar:"
    echo $scalar
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
    GNU bash 3.2.39
    
    -----
     Contents of entire array:
    x
    x y z
    x y z
    
    -----
     Contents of scalar:
    x y z
    However, it might be surprising to see how another shell, zsh, performs:
    Code:
    % zsh s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
    zsh 4.3.6
    
    -----
     Contents of entire array:
    x y z
    x y z
    x y z
    
    -----
     Contents of scalar:
    x y z
    One might find interesting examples of array use with bash at: Arrays

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #6
    Linux Newbie Nagarjuna's Avatar
    Join Date
    Feb 2011
    Posts
    122
    Cool, thanks for the alternative examples guys. I'm really new to scripting/programming and trying to get my feet wet, so your input helps greatly.. Nothing worse than doing more work to achieve something that you could of with half the effort!

    I'll play around with the examples when I get home. Thanks again.

  8. #7
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by stokes View Post
    I've been a Linux user for quite a few years now, but have never seen this kind of syntax used before. Usually the following more simple syntax is used to with with arrays in shell scripts.

    Code:
    #!/bin/bash
    
    array="~tristan/scripts/access ~tristan/scripts/backup ~tristan/scripts/newuser ~tristan/scripts/pacman"
    
    echo ${array}
    tar czvf backup-$(date +%d-%b-%y).tar ${array}

    That will not work if any filenames contain whitespace; using an array, it will work with any filenames.

  9. #8
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by Nagarjuna View Post
    When running the tar command, be sure you reference the array like so:

    Code:
    tar czvf backup-"$(date +%d-%b-%y)".tar ${array[@]}

    That will fail if any filenames contain whitespace. Use:
    Code:
    tar czvf backup-$(date +%Y-%m-%d).tar "${array[@]}"

  10. #9
    Linux Newbie Nagarjuna's Avatar
    Join Date
    Feb 2011
    Posts
    122
    Quote Originally Posted by cfajohnson View Post

    That will fail if any filenames contain whitespace. Use:
    Code:
    tar czvf backup-$(date +%Y-%m-%d).tar "${array[@]}"
    Heh, I figured this out the hard-way just a couple days ago! Thanks for the clarification, cfajohnson.
    “Things derive their being and nature by mutual dependence and are nothing in themselves.”

Posting Permissions

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