help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Loop over,


From: Greg Wooledge
Subject: Re: [Help-bash] Loop over,
Date: Tue, 6 Sep 2016 09:45:48 -0400
User-agent: Mutt/1.4.2.3i

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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]