[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Loop over,
From: |
Val Krem |
Subject: |
Re: [Help-bash] Loop over, |
Date: |
Sat, 10 Sep 2016 01:16:16 +0000 (UTC) |
Hi Greg,
Thank you so much for your advice and go through my broken and vague script. I
understand my weaknesses and I am open to learn. I have to admit that I was
also less descriptive in describing my goal.
I got your messages
1) use of double quotes
2) use of an array
3) use of all-caps is not good idea
Here is my situation,
I have several folders and I put the names of the folders in in the array.
name=(week1 week2... week52).
Go to each folder and pick files and copy files to some temporary folders. My
problem is that the names of the folders listed in the array do not have the
same path. Either they are in
rdat1=/data/year/"${dat}" # the first folder
or
rdat2=/data/year/month/"${dat}" # second folder
Below is the revised script and I would appreciate if you help me
from the lines 17 to 21.
########### REVISED VERSION ###################
1 #!/bin/sh
2
3 names="week1 week2... week52 "
4 for S in "$names"
5 do
6 echo " Start "
7 dat="$S" # get the name of the weeks from the array
8
9 # DEFINE the path where the folders are located
10
11 rdat1=/data/year/"${dat}" # the first path
12 rdat2=/data/year/month/"${dat}" # the second path
13 tmp=/data/result/"${dat}" # temporary working folder
14
15 mkdir -p "${tmp}" # my working directory
16
17 shopt -s nullglob
18 files=("$rdat1"/*.zip)
19 if (( ! address@hidden )); then
20 files=("$rdat2"/*.zip)
21 fi
22
23 cp "${file}" "${tmp}"
24 unzip "${tmp}"/'*.z'
25 etc1 #### this other
steps that does some editing of data
26 done
Thank you in advance
On Tuesday, September 6, 2016 9:16 AM, Greg Wooledge <address@hidden> wrote:
You have two MAJOR issues:
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/BashFAQ/005
And your question, which you would NEVER have got to work because of the
major issues breaking everything else:
http://mywiki.wooledge.org/BashFAQ/004
Read those. Once you understand those, then you can move on.
On Tue, Sep 06, 2016 at 02:59:23AM +0000, Val Krem wrote:
> I have one script that works fine for a particular job. My problem is now I
> have to repeat running this script several time to do the same job for
> different sets of data that are located in different directories (folders).
> My files are in two separate folders.
So you want to run a script once for each file in a directory? With the
filename as an argument? No problem:
for f in *; do myscript "$f"; done
> 1) How can I tell the script if the file is not in one folder then look for
> another folder?
You're being extremely vague. It's very hard to answer vague questions.
Do you mean, "If the current directory is empty, then I want the script
to cd to /dir2"?
If so, then here is one way:
shopt -s nullglob
files=(*)
if (( address@hidden == 0 )); then
cd /dir2 || exit
fi
> 2) How could I run 52, in this case, times in one touch?
If it's "once per file", then the for loop that I showed above is all
you need.
> # ! /bin/bash
Incorrect.
> NAMES="Week1 week2... week52 "
Incorrect use of a string variable to hold a list. Use an array instead.
Incorrect use of all-caps in a non-environment variable name.
Incorrect pasting of your script. Clearly this is not your actual script.
It's just some crap you typed into an email. This is NOT productive
behavior on your part. If you actually want help with a script, show the
script. Not something that kinda sorta vaguely resembles the script.
(It's OK to delete irrelevant sections, but it is NOT OK to retype the
relevant lines incorrectly.)
Finally, if the names are supposed to be ACTUAL FILES that EXIST in a
directory, then you wouldn't hardcode them in the script at all. You
would simply iterate over all of the files that are present.
> for S in $NAMES
Incorrect expansion of a string variable that is trying to do the job of
an array.
If you are really iterating over files, then use:
for s in *
or
for s in week*
If you are hard-coding a list of iteration values inside the script, instead
of using existing filenames, then use an array.
names=(
alice bob charlie
delta echo foxtrot
"billy joe" bubba
yoink zebra
)
for s in "address@hidden"; do
...
done
If you are (for some inscrutable reason) hard-coding a list that you want
to generate programmatically, then maybe you want something like this:
names=( week{1..52} )
This is a bash brace expansion. It is a shortcut for typing out
repetitive words in a list.
It's incredibly difficult to guess what you are actually doing.
> export S
WHY? Is there a child process that needs this variable to be present in
the environment?
> [ -d ${tmp} ]
Quoting failure. Bad!
[ -d "$tmp" ]
> && echo "Directory Exists" || mkdir ${tmp} # my working directory
Just use mkdir -p. Also a second quoting failure.
mkdir -p "$tmp"
> file=${rdat1}/*.zip OR file=${rdat2}/*.zip ### here is my
> problem!!!!
USE AN ARRAY.
Also you have another quoting failure (although it's masked by the failure
to use an array).
shopt -s nullglob
files=("$rdat1"/*.zip)
if (( ! address@hidden )); then
files=("$rdat2"/*.zip)
fi
Note that the variable expansion is QUOTED, but the glob that we want to
expand is not. This is critical.
> cp ${file} ${tmp}
QUOTING FAILURES!
> unzip ${tmp}/'*.z'
QUOTING FAILURE!
I have deleted the rest. It's just not worth my time at this point.