Is there a more efficient way to run this? I am especially interested in a way which perhaps does not require a for
loop...
shopt -s nullglob
file=
declare -a ARRAY1
ARRAY1=( ~lettus_*.sh )
for file in ${ARRAY1[@]}
do
##
## Get <name> from /some/path/lettus_name.sh
##
# remove .sh extension
file=${file%.sh}
# remove full path up to and including lettus_
file=${file//*lettus_}
echo $file
done
shopt -u nullglob
1 Answer 1
I'm assuming that
file=${file//*lettus_}
is supposed to befile=${file##*lettus_}
.Don't do
file=
. There is no reason to declare thefile
variable before-hand. Using it is a the loop variable automatically creates it.Get rid of the temporary
ARRAY1
variable (which fyi is not a good variable name). Just dofor file in lettus_*.sh
. In fact, since you don't quote${ARRAY1[@]}
, word-splitting would make your program fail in some cases.Why do you want a more efficient way? This should run in a split-second unless you have a million files.
-
\$\begingroup\$ Yes. Need to change to
file=${file##*lettus_}
. Thanks. And you are right, it will be very fast. I just thought I was perhaps scripting this naively..... \$\endgroup\$Felipe Alvarez– Felipe Alvarez2016年01月12日 02:23:56 +00:00Commented Jan 12, 2016 at 2:23 -
1\$\begingroup\$ Well "naively" doesn't just have to do with performance. All programming -- even "simple" shell scripting -- is a trade-off among many competing factors, two of which are speed and code clarity. In most cases, clarity wins out :) \$\endgroup\$gardenhead– gardenhead2016年01月12日 02:43:16 +00:00Commented Jan 12, 2016 at 2:43