This query is a part of a much more lengthy script.
I wish to look for all the files in a folder named "data" which in this case has two files i.e. plan.war and agent.properties . For all the files found under data I wish to ask the user as to where they wish copy the files to.
Below, is my script for the same.
more run.sh
#!/bin/bash
cd data
find . -type f \! -name test.tar | $AWK -F/ '{print $NF}' | while IFS= read -r entry; do
echo "FILE_CURRENT_DIR:"$entry
read -p 'Where do you want to copy $entry to? Please enter the complete directory location: ' cppath
echo "cp -R $entry $cppath/"
cp -R $entry $cppath/
done
However, instead of asking me for input the script considers the second file found under data folder at the target location for the copy and thus fails.
Kindly see the problematic output below.
Can you please help me get user prompt for every file found under data ?
You have two read , so you have to use a different file descriptor to make it work.
Another approach is to use a for loop instead:-
for file in data/*
do
[[ "$file" =~ "test.tar" ]] && continue;
read -p "Where do you want to copy ${file} to? enter absolute path: " cp_path
echo "cp -R $file ${cp_path}/"
done
Replacing find with for will not traverse sub-directories. Use the find based approach if you want to traverse sub-directories and work on files in them.
Can you please tell me why is it failing when I try the same.
#!/bin/ksh
. ./deploy.profile
set -x
found_flag=0
searchdir=/tmp/moht
CWD=$(pwd)
find $searchdir"/PRESENT/data" -type f \! -name client.tar | $AWK -F/ '{print $NF}' > ${CWD}/out.txt
while IFS= read -r entry; do
found_flag=0
if [ $found_flag -eq 0 ]; then
read -p "Where do you want to copy $entry to? Please enter the complete directory location: " -u 3 cppath
echo "cp -R $entry $cppath/"
cp -R $entry $cppath/
fi
echo "Displaying the value of found_flag outside while loop:$found_flag"
done 3<&0 < ${CWD}/out.txt
Error:
Hope this works with the KSH shell as well. Does it ?
read is a shell built-in. The KSH built-in has different options to the Bash one. Is there any reason you changed your script from Bash to the Korn shell?
The reason for changing to KSH from BASH was my bash version is 10 yrs old and I cannot update it as I m not the system admin. I need my variable to hold global value so it persists its value outside the loop. Refer to my thread here for details.
Can you propose any solution to the error I m getting as shown in RED BOLD this POST 8.