ae to add some annotations.
The following two lines use a subshell $( : )
and a pipe (which in fact ends up with more subshells.
baseaddr="$(echo $ip | cut -d. -f1-3)"
lso="$(echo $ip | cut -d. -f4)"
You just want to cut the IP into pieces.
s own facilities. There is a parameter expansion doing this easily.
No subshell. No overhead. And very fast.
Now you are using ls
Like above unneeded subshells and forks.
But a source of subtle failures is the use of lsls
gives NOT the filenames, it gives formatted
And this output is influenced by LOCALE too. It is NOT guaranteed that this will work on every machine.
# very bad!
for f in $(ls -altr $cwd/ | awk '/.txt/'); do
# do instead
for f in $cws/*.txt; do
echo filename is $f
This works everywhere and is much more faster. You might check the setting of glob
before. (Is enabled by default, but we should never rely on any assumptions.)
Another way to deal with filenames very secure is to use the proper tool for these issues: find
Get used to it. It is fast, reliable and outputs file names. Nothing else.
find $cwd -iname '*.txt' find
searches from $cwd all directories and lists all files ending wit ".txt". It understands a lot of tests one can do with filenames. In fact you can combine any tests with logical operators like -a
r and negations !
tests filenames case insensitive (i
) . Hence all files ending with any combination of [Tt][Xx][Tt] will be found.