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 ...
- 03-29-2011 #1Just Joined!
- Join Date
- Jan 2011
- Posts
- 19
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
- 03-29-2011 #2
When running the tar command, be sure you reference the array like so:
Writing out the array variable like above tells tar to run against each individual entry in the array.Code:tar czvf backup-"$(date +%d-%b-%y)".tar ${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:
I hope this helps. If not, let me know and I'll try to assist further.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[@]}
- 03-29-2011 #3Just Joined!
- Join Date
- Jan 2011
- Posts
- 19
- 03-30-2011 #4
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
- 03-30-2011 #5Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
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:
producing: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
However, it might be surprising to see how another shell, zsh, performs: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
One might find interesting examples of array use with bash at: ArraysCode:% 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
Best wishes ... cheers, drlWelcome - 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 )
- 03-30-2011 #6
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.
- 04-02-2011 #7
- 04-02-2011 #8
- 04-02-2011 #9


Reply With Quote
