I would like to question the use of for-loops to cycle through arbitrary lists.
As long as a list is of fixed length there is nothing problematic with a for-loop:
The problem arises when shell-regexps ("globs") are used to expand to a list of files:
In this case there is a chance of the script failing: the glob is expanded to a list of files before executing the commandline (this is shell standard) and - depending on the numbers of files it expands to - the command line could get longer than the maximum supported command line on POSIX systems (which is 4k characters, see sys/limits.h). In this case you run a chance of the shell terminating the script with a "command line too long" (or similar) error. This is true for ksh, bash and sh alike, probably for most of the other shells too.
What can be done to avoid this? The solution is quite simple: replace the "for x in ..."-construction with an equivalent while-loop and feed that with a pipeline. The second example from above could safely be rewritten as:
Hello all,
I have a question. Please help me to populate an array with the arguments passing to a Shell scripts.
For example when I call "abc.sh a1 a2 a3" args (a1, a2, ...) recieved in an Array inside the abc.sh
arr = a1
arr = a2
and so on...
Thanks in advance, (2 Replies)
Hello ( sorry newbie question )
I don't understand something im trying to make simple alias that takes 1 arg but it don't gives me the desire result
here is what I have :
stlist | awk '{print "ls -l "$2}'
now I want to translate it to alias that takes instead of the $2 one arg
so I did : ... (4 Replies)
My program usage takes the form for example;
$ theApp 2 "one or more words"
i.e. 3 command line arguments; application name, an integer, some text
My code includes the following 4 lines:
int anInteger;
char words;
sscanf(argv, "%d", &anInteger);
sscanf(argv, "%s", &message);
Based... (2 Replies)
I am trying to print command line arguments one per second. I have this
while
do
echo "6"
shift
echo "5"
shift
echo "4"
shift
echo "3"
shift
echo "2"
shift
echo "1"
shift
done (2 Replies)
I have this while loop and at the end I am trying to get it to tell me the last argument I entered. And with it like this all I get is the sentence with no value for $1. Now I tried moving done after the sentence and it printed the value of $1 after every number. I don't want that I just want... (2 Replies)
Hello,
i have a problem. I must write a script, which wants 2 arguments.
for example:
./test.sh x.txt y.txtit must be write x.txt in y.txt
and when i give 1 or no argument like
/.test.sh x.txtmust this give a error message like: SYNTAX <inputfile> <outputfile>
my solution is... (5 Replies)
I am trying to write a simple shell script that will take certain arguments (numerical values) and plug each one into a nawk command.
I thought I would need to use for args x y z but i get syntax errors:
for args 16 1 3 25 31 41
do
nawk -F, '{if($10==$ &&... (8 Replies)
Store args passed in array but not the first 2 args.
# bash
declare -a arr=("$@")
s=$(IFS=, eval 'echo "${arr}"')
echo "$s"
output:
sh array.sh 1 2 3 4 5 6
1,2,3,4,5,6
Desired output:
sh array.sh 1 2 3 4 5 6
3,4,5,6 (2 Replies)
Heya
Tooltip: Parsing (getopts) for -u successfully sets mode=umnt, but case umnt is not executed,
instead it either executes/show help or regular mount screen.
I had copy pasted the structure of a getopts 'structure' from Man Page for getopts (posix Section 1) - The UNIX and Linux Forums... (1 Reply)
Hi,
I'm using a while loop for a given command "bowtie2" with several parameters.
mkdir clean
paste <(ls --quote-name ./qc/sg_*_R1_val_1.fq.gz) <(ls --quote-name ./qc/sg_*_R2_val_2.fq.gz) |sed 's/"./-1 ./' | sed 's/gz"\t/gz\t -2 /' | sed 's/"//g' |\
while read args ; do
... (5 Replies)
Perl::Critic::Policy::InputOutput::ProhibitReadlineInForUser(Contributed Perl DocPerl::Critic::Policy::InputOutput::ProhibitReadlineInForLoop(3pm)NAME
Perl::Critic::Policy::InputOutput::ProhibitReadlineInForLoop - Write "while( $line = <> ){...}" instead of "for(<>){...}".
AFFILIATION
This Policy is part of the core Perl::Critic distribution.
DESCRIPTION
Using the readline operator in a "for" or "foreach" loop is very slow. The iteration list of the loop creates a list context, which causes
the readline operator to read the entire input stream before iteration even starts. Instead, just use a "while" loop, which only reads one
line at a time.
for my $line ( <$file_handle> ){ do_something($line) } #not ok
while ( my $line = <$file_handle> ){ do_something($line) } #ok
CONFIGURATION
This Policy is not configurable except for the standard options.
AUTHOR
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
COPYRIGHT
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license
can be found in the LICENSE file included with this module.
perl v5.14.2 2012-06-07 Perl::Critic::Policy::InputOutput::ProhibitReadlineInForLoop(3pm)