Help on for loop in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on for loop in bash
# 1  
Old 06-19-2016
Lightbulb Help on for loop in bash

Hi,

In the code "for loop" has been used to search for files (command line arguments) in directories and then produce the result to the standard output. However, I want when no files are named on the command line, it should read a list of files from standard input and it should use the command line arguments when given.

It can be done with "read" command by modifying the code, however, I am not able to make it work with both. I mean I want the code to work for both ways (reading standard input when no command line arguments are given and reading command line arguments when given).

Don't want to go for another block of "for loop" for both the conditions. Any suggestions would be highly appreciated.....Thanks.

Code:
#! /bin/sh
#
# Search for one or more ordinary files or file patterns on a search
# path defined by a specified environment variable.
#
# The output on standard output is normally either the full path
# to the first instance of each file found on the search path,
# or "filename: not found" on standard error.
#
# The exit code is 0 if all files are found, and otherwise a
# nonzero value equal to the number of files not found (subject
# to the shell exit code limit of 125).
#
# Usage:
# pathfind [--all] [--?] [--help] [--version] envvar pattern(s)
#
# With the --all option, every directory in the path is
# searched, instead of stopping with the first one found.

IFS='
'
OLDPATH="$PATH"
PATH=/bin:/usr/bin
export PATH
error( )
{
echo "$@" 1>&2
usage_and_exit 1
}

usage( )
{
echo "Usage: $PROGRAM [--all] [--?] [--help] [--version] envvar pattern(s)"
}

usage_and_exit( )
{
usage
exit $1
}

version( )
{
echo "$PROGRAM version $VERSION"
}

warning( )
{
echo "$@" 1>&2
EXITCODE=`expr $EXITCODE + 1`
}

all=no
envvar=
EXITCODE=0
PROGRAM=`basename $0`
VERSION=1.0

while test $# -gt 0
do
  case $1 in
	--all | --al | --a | -all | -al | -a )
	all=yes
	;;
	--help | --hel | --he | --h | '--?' | -help | -hel | -he | -h | '-?' )
	usage_and_exit 0
	;;
	--version | --versio | --versi | --vers | --ver | --ve | --v | \
	-version | -versio | -versi | -vers | -ver | -ve | -v )
	version
	exit 0
	;;
	-*)
	error "Unrecognized option: $1"
	;;
	*)
	break
	;;
  esac
  shift
done

envvar="$1"
test $# -gt 0 && shift
test "x$envvar" = "xPATH" && envvar=OLDPATH

dirpath=`eval echo '${'"$envvar"'}' 2>/dev/null | tr : ' ' `

# sanity checks for error conditions
if test -z "$envvar"
  then
  error Environment variable missing or empty
  elif test "x$dirpath" = "x$envvar"
  then
  error "Broken sh on this platform: cannot expand $envvar"
  elif test -z "$dirpath"
  then
  error Empty directory search path
  elif test $# -eq 0
  then
  exit 0
fi

# loop over argument files or patterns and directories in the search path

for pattern in "$@"
do
  result=
  for dir in $dirpath
  do
    for file in $dir/$pattern
    do
      if test -f "$file"
      then
      result="$file"
      echo $result
      test "$all" = "no" && break 2
      fi
    done
  done
  test -z "$result" && warning "$pattern: not found"
done

# Limit exit status to 125
test $EXITCODE -gt 125 && EXITCODE=125
exit $EXITCODE

# 2  
Old 06-19-2016
Usually you'd test for, say, missing cmd line parameters, e.g. $# = 0, and then read your file names from stdin, else run through your for loop.

However, not having dug deep into the logics, I'm not sure that your existing for loop has anything to do as $@ i.e. all parameters have been eaten up by the shifts in the while loop?
# 3  
Old 06-19-2016
Hi RudiC,

Thanks for your reply.

The "shift" in "while loop" is for parameters ([--all] [--?] [--help] [--version]), not for variables "envvar" and "pattern". The "for loop" is looping over "$@" and collecting the file names as command line arguments (which will fall under "pattern").

I already tested with "test", however, that works for reading file names from standard input, however, at the same point "for loop" stops working while reading command line arguments.

for example:

If I use the below "test" statement (note: needed to replace "$@" with the variable "pattern" used for "read") to read the files from standard input then it will do this job fine, however, at the same point it will not work for reading file names from command line arguments. That is where I need the fix for this. I want it to work for reading the file names from standard input as well as from command line arguments.

Code:
test $# -eq 0 && read pattern
for pattern in "$pattern"
do
  result=
  for dir in $dirpath
  do
    for file in $dir/$pattern

# 4  
Old 06-19-2016
Like this?
Code:
if test $# -eq 0
then
  read pattern
else
  pattern=$@
fi
for pattern in "$pattern"
do
...

# 5  
Old 06-19-2016
There are several things that seem strange in your script, but some of what I'm seeing may just be a nomenclature problem. You have a variable named pattern and you have shown us the code segment:
Code:
test $# -eq 0 && read pattern
for pattern in "$pattern"
do
  result=
  for dir in $dirpath
  do
    for file in $dir/$pattern

which (even ignoring the missing do and three done commands) can't possibly be what you want. If $pattern expands to a list of files or a list of pathnames, the first time through the loop started with:
Code:
for pattern in "$pattern"

will set pattern to be the entire list and you will never go through that loop more than once.

And, if $pattern contains any filename pattern special characters (e.g., ?, *, [list], etc.), they will not be expanded inside double quotes.

When you enter patterns on the command line when you invoke your script are the patterns quoted (so the shell doesn't perform pattern matching before invoking your script)? When you read patterns from standard input, are they really patterns or are they just pathnames of files?

Last edited by Don Cragun; 06-19-2016 at 06:23 PM.. Reason: Fix misleading statement.
# 6  
Old 06-23-2016
Hi Don,

Sorry for the delay in response. Before you further analyse the code I would request you to go through the POST#1 (entire script and the purpose has been stated there).

Here is the real code (to search for file-names (patterns) in the PATH). Here it reads the command line arguments for variable "pattern" (file-names). It search for every provided files in the PATH once if we don't use the option "all" (refer the usage) in POST#1. If we use the option "all" then it go through the loop again and look for the same files in other directories in the PATH

Code:
for pattern in "$@"
do
  result=
  for dir in $dirpath
  do
    for file in $dir/$pattern
    do
      if test -f "$file"
      then
      result="$file"
      echo $result
      test "$all" = "no" && break 2
      fi
    done
  done
  test -z "$result" && warning "$pattern: not found"
done

What I would like to achieve from this script is here:

I want the script to read the file-names under variable "pattern" from the standard input if no command line arguments are provided and read the command line arguments when provided. However, I am not able to achieve both at the same time.

Here are your answers you asked in your post:

1. I don't quote the patterns.
2. When I read the file-names (patterns) from standard input.

Last edited by Ra26k; 06-23-2016 at 12:03 PM..
# 7  
Old 06-23-2016
We may have a language barrier here. I am asking questions in shell, your answering in English.

If you have a bunch of files with names ending with the four characters .txt, I can use the shell filename matching pattern *.txt to get a list (produced by the shell before it invokes your script) of all of the files matching that filename matching pattern. The data passed as command-line arguments to your script will be filenames, not patterns. But, if the command-line argument was quoted ("*.txt"), then the arguments that your script would get would be filename matching patterns; not filenames. You have said that you aren't quoting your arguments, so using a variable named pattern to hold these filenames (not patterns) is misleading and confusing to people trying to understand your script.

But, when you're reading from standard input, if you're just typing text that will be read by your script, the shell will not perform filename matching pattern expansions to filenames of any patterns you type into your script; so if you are expecting the shell to expand filename matching patterns on command-line operands, you must just type in filenames; not filename matching patterns for lines that type into your script that you want it to read from standard input.

The following script shows you how you can read command-line operands or (if no operands are present after option parsing has been completed) or lines from standard input:
Code:
#!/bin/sh
if [ $# -gt 0 ]
then	for arg in "$@"
	do	printf '%s\n' "$arg"
	done
else	while IFS= read -r line
	do	printf '%s\n' "$line"
	done
fi | while IFS= read -r file
do	printf 'operand or stdin line #%d: "%s"\n' $((++cnt)) "$file"
done

This will work with any shell that uses basic POSIX shell requirements. It won't work with a traditional Bourne shell (since it doesn't support $((expression)) arithmetic substitutions).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Speed up bash loop?

I am running the below bash loop on all the files of a specific type (highlighted in bold) in a directory. There are 4 awk commands that use the input files to search another and look for a match. The input files range from 27 - 259 and are a list of names. The file that is searched is... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. Shell Programming and Scripting

Bash Shell loop - Help !

Dear all Linux lover, I am a new learner to Bash Shell script and I would like to writing a script to to repeat my script. This mean I would like to have multiple same of result after running the .sh. ####### TIMES_NO=0 echo -n "Please enter the number for times to repeat ?" read... (10 Replies)
Discussion started by: Rocky888
10 Replies

3. Shell Programming and Scripting

Bash For Loop Help

This is a short program I wrote to search through a directory and move files containing the keyword to a folder. #!/bin/bash echo 'What is the directory?' read DIR echo -e '\n' echo 'What is the keyword?' read KEY echo -e '\n' cd $DIR rmdir 'relevant_files' mkdir 'relevant_files'... (5 Replies)
Discussion started by: zenyoul
5 Replies

4. UNIX for Dummies Questions & Answers

Help with 3 variable bash loop

Hi all! I think someone might be able to solve my problem pretty easily. I am trying to run a bash loop with 3 variables. I know how to do: for var1 in `cat list1`; do for var2 in `cat list2`; do for var3 in `cat list3`; command var1 var2 > var3; done; done; done However, this will run all... (4 Replies)
Discussion started by: torchij
4 Replies

5. Shell Programming and Scripting

If loop in bash

Hello, I have a script that runs a series of commands. Halfway through the script, I want it to check whether everything is going alright: if it is, to proceed with the script, if it isn't to repeat the last step until it gets it right. My code so far looks like this, simplified a bit: ... (3 Replies)
Discussion started by: Leo_Boon
3 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Problem with for loop in bash

I'm trying to do a script where I want to see if all users home directories are only writable by owner. However, in my script I do not know how to implement the for loop so that all directories are checked. In mine, I am only checking the permissions for the first directory found. I do know that a... (3 Replies)
Discussion started by: detatchedd
3 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

10. Shell Programming and Scripting

Simple loop in Bash

Hi, I want to do a simple loop where I have one column of text in a file and I want the loop to read each line of the file and do a simple command. The text file will be something like this: hostname1 hostname2 hostname3 hostname4 I am using Bash and have already come up with this to... (1 Reply)
Discussion started by: BrewDudeBob
1 Replies
Login or Register to Ask a Question