I have a list of files in /tmp/drop
directory. I have taken the essential element of my question into test script zz.sh
. I have taken a part of the script and put it here. These are the files and the lines listed - and when it given to while loop - how does the variable file
gets assigned only the file name and not the various other strings in those lines?
Any help would be appreciated and would be helpful to others too.
machine001:/home/aaa999999> ls -l /tmp/drop | grep "2017-01-29" | egrep '\.gz$' | tail -10
+ ls -F -l /tmp/drop
+ tail -10
+ grep 2017-01-29
+ egrep \.gz$
-rw-r--r-- 1 user1001 user1001 20 Jan 29 2017 pattern101_worker-2_2017-01-29-21:33:13.888.complete.gz
-rw-r--r-- 1 user1001 user1001 20 Jan 29 2017 pattern101_worker-2_2017-01-29-21:48:14.632.complete.gz
-rw-r--r-- 1 user1001 user1001 20 Jan 29 2017 pattern101_worker-2_2017-01-29-22:03:19.098.complete.gz
-rw-r--r-- 1 user1001 user1001 20 Jan 29 2017 pattern101_worker-2_2017-01-29-22:18:19.416.complete.gz
-rw-r--r-- 1 user1001 user1001 20 Jan 29 2017 pattern101_worker-2_2017-01-29-22:33:19.878.complete.gz
-rw-r--r-- 1 user1001 user1001 20 Jan 29 2017 pattern101_worker-2_2017-01-29-22:48:25.636.complete.gz
-rw-r--r-- 1 user1001 user1001 20 Jan 29 2017 pattern101_worker-2_2017-01-29-23:03:26.515.complete.gz
-rw-r--r-- 1 user1001 user1001 20 Jan 29 2017 pattern101_worker-2_2017-01-29-23:18:28.279.complete.gz
-rw-r--r-- 1 user1001 user1001 20 Jan 29 2017 pattern101_worker-2_2017-01-29-23:33:33.059.complete.gz
-rw-r--r-- 1 user1001 user1001 20 Jan 29 2017 pattern101_worker-2_2017-01-29-23:48:33.841.complete.gz
machine001:/home/aaa999999> cat zz.sh
+ cat zz.sh
#!/bin/bash
set -x
DROP_DIR=/tmp/drop
ARCHIVE_DIR=/tmp/arch
YESTERDAY="2017-01-29"
# move from drop dir
ls -1 ${DROP_DIR} | grep ${YESTERDAY} | egrep '\.gz$' | while read file; do
mv ${DROP_DIR}/$file ${ARCHIVE_DIR}
echo 'File name is ' ${file}
done
mv /tmp/drop/*2017-01-29*.gz /tmp/arch
? – pfnuesel Jan 31 '18 at 00:53ls -l
(letter ell) while in the script it appears to bels -1
(digit one) - different options, different behaviors. Regardless, please see Why not parsels
? – steeldriver Jan 31 '18 at 01:01for file in "$DROP_DIR/*$YESTERDAY*.gz" ; do ... ; done
- parsingls
is not only potentially dangerous, it isn't even necessary. Also, thatwhile read
loop is in a pipe subshell, so can't affect the environment or variables of the parent script (see https://unix.stackexchange.com/questions/9954/why-is-my-variable-local-in-one-while-read-loop-but-not-in-another-seemingly for why). – cas Jan 31 '18 at 04:58