0
declare -a list=(data backups dailystatus inbound reports )

ls ${list[0]}/${list[1]}/${list[2]}/${list[3]}/${list[4]}/

I need to check daily backups are working or not similar this I have 2 more locations for checking backup. Can someone help me to find out the better way than this?

jrbedard
  • 3,662
  • 5
  • 30
  • 34
kreench
  • 1
  • 1
  • 3
    Please clarify your specific problem or add additional details to highlight exactly what you need. – Cyrus Jan 14 '17 at 11:01
  • 1
    Why are you storing the path components as separate array elements, instead of a single directory path? – chepner Jan 14 '17 at 13:42
  • It might be a start to describe *why* your current approach is inadequate, how it fails, how to reproduce that failure, and how a potential solution should be evaluated for correctness. (A better explanation of why you're writing code like this in the first place would help too; in general, [programmatic use of `ls` is error-prone, and consequently frowned on](http://mywiki.wooledge.org/ParsingLs)). – Charles Duffy Jan 14 '17 at 17:18
  • 1
    ...that said, generally, one would store one path to an array element: `list=( /data/backups/{daily,weekly,monthly}status/inbound/reports )`, for instance, will give you an array with one dailystatus path, one weeklystatus path, and one monthly status path -- much more sensible than splitting the path elements into an array element per each for no obvious reason (and if you *have* a reason, **include it in the question!**). – Charles Duffy Jan 14 '17 at 17:23

2 Answers2

0

from Join elements of an array?

#!/bin/bash
list=(data backups dailystatus inbound reports )
bar=$(printf "/%s" "${list[@]}")
echo $bar
Community
  • 1
  • 1
dormi330
  • 1,273
  • 11
  • 18
-1

One way(assuming you do not have any multi worded directory):

x=${list[@]}
ls ${x// /\/}

Concatenate all array elements into variable x and then replace the space with a /

Guru
  • 16,456
  • 2
  • 33
  • 46
  • This fails if a directory has a space in its name. – chepner Jan 14 '17 at 13:43
  • @chepner : that was precisely what I had mentioned in brackets – Guru Jan 14 '17 at 16:27
  • 1
    Why would you give an answer you know is buggy (doesn't cover the entire input domain -- that input domain, in this case, being "possible filenames on UNIX") when you could fix it instead, and give an answer that's correct? – Charles Duffy Jan 14 '17 at 17:17