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.
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?
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.
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:
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:
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.
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
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.
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:
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).
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)