Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2316  / 1 Year ago, fri, may 5, 2023, 6:17:31

I made this simple script to copy a file recursively to many sub-directories. Here I am showing the problem for 3 sub-directories. I have 3 sub-directories and 2 files in a directory as follows



0.003/  0.007/  0.015/  program.cpp  driver.sh*


I want to copy program.cpp to all these sub-directories using myscript.sh shown below:



    #!/bin/bash
mydir=`pwd`

for i in `ls -la $mydir | grep "[0-9]$" | awk '{print $NF}'`
do
if [ -e $mydir/$1 ]
then
echo "cp -i $1 $i/"
else
echo "File "$1" not found in current directory"
fi
done


I have put the cp inside echo to test the code. The output is strange,



cp -i program.cpp 32/
cp -i program.cpp 0.003/
cp -i program.cpp 0.007/
cp -i program.cpp 0.015/


The output of ls -la



total 32K
drwxr-xr-x. 5 gulu workg 4.0K Nov 29 2013 .
drwxr-xr-x. 7 gulu workg 4.0K Nov 29 2013 ..
drwxr-xr-x. 2 gulu workg 4.0K Nov 26 19:09 0.003
drwxr-xr-x. 2 gulu workg 4.0K Nov 26 19:09 0.007
drwxr-xr-x. 2 gulu workg 4.0K Nov 26 19:09 0.015
-rw-r--r--. 1 gulu workg 4.2K Nov 29 2013 program.cpp
-rwxr-xr-x. 1 gulu workg 982 Nov 26 08:22 driver.sh


The output of ls -la $mydir | grep "[0-9]$" | awk '{print $NF}'



0.003
0.007
0.015


There are only three values in i then how is it producing four output? Anyway I have solved the problem with an additional condition [ -d $mydir/$i ]. But my question is how the script is producing the first line cp -i driver.sh 32/?


More From » bash

 Answers
1

This illustrates why you should NEVER rely on parsing the output of the ls command to iterate over directory contents - use a simple shell glob instead e.g. to match any filename ending in a digit



for f in $mydir/*[0-9]
do
if [ -e $mydir/$1 ]
.
.
done


As to how it is producing the line cp -i driver.sh 32/, you can see that when you grep for digits in the output of ls -la, it is matching the total: 32K line as well as the wanted directory names.


[#28208] Sunday, May 7, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
otatorm

Total Points: 218
Total Questions: 113
Total Answers: 124

Location: British Indian Ocean Territory
Member since Tue, Feb 22, 2022
2 Years ago
otatorm questions
;