I am trying to select a file in bash and save it to a directory. The below does run but no selected file is saved. Thank you :).
bash
# select file
printf "please select a file to analyze with entered gene or genes \n"
select file in $(cd /home/cmccabe/Desktop/NGS/API/test/bedtools;ls);do break;done
echo $file "will be used to filter reads, identify target bases and genes less than 20 and 30 reads, create a low coverage bed for vizulization, and calculate 20x and 30x coverage for the entered gene(s)"
logfile=/home/cmccabe/Desktop/NGS/API/test/process.log
for file in /home/cmccabe/Desktop/NGS/API/test/bedtools/$file; do
echo "Start selcted file creation: Panel: ${FUNCNAME[0]} Date: $(date) - File: $file"
bname=$(basename $file)
pref=${bname%%.txt}
while read -r file; do mv "$file" /home/cmccabe/Desktop/NGS/panels/reads/${pref}_OTHER.txt ; done
echo "End selected file creation: $(date) - File: $file"
done >> "$logfile"
Two comments without being able to dive deeper into your logics nor being able to test anything:
Using the file variable in several places ( select file ... ; for file ... ; read -r file ... ) you overwrite the previously defined value. This may not be want you want...
in your while read -r ... loop (BTW, why a loop here?) without redirection you read from the terminal. Is that what you want ?
No, I am just trying to selct one file in the directory and save it in a new directory. You are correct a loop is not needed.
Search directory with files
file1.txt
file2.txt
file3.txt
file1.txt is selected and saved in the new directory (/home/cmccabe/Desktop/NGS/panels/reads/)
So do I just need to read that selection into a variable and pass that to the new directory, or is there a better way? Thank you :).
Maybe:
# select file
printf "please select a file to analyze with entered gene or genes \n"
select file in $(cd /home/cmccabe/Desktop/NGS/API/test/bedtools;ls);do break;done
echo $file "will be used to filter reads, identify target bases and genes less than 20 and 30 reads, create a low coverage bed for vizulization, and calculate 20x and 30x coverage for the entered gene(s)"
logfile=/home/cmccabe/Desktop/NGS/API/test/process.log
for file in /home/cmccabe/Desktop/NGS/API/test/bedtools/$file; do
echo "Start selcted file creation: Date: $(date) - File: $file"
bname=$(basename $file)
pref=${bname%%.txt}
mv "$file" /home/cmccabe/Desktop/NGS/panels/reads/${pref}_OTHER.txt
echo "End selected file creation: $(date) - File: $file"
done >> "$logfile"
error
file1.txt will be used to filter reads, identify target bases and genes less than 20 and 30 reads, create a low coverage bed for visualization, and calculate 20x and 30x coverage for the entered gene(s)
mv: cannot move �/home/cmccabe/Desktop/NGS/API/test/bedtools/file1.txt� to �/home/cmccabe/Desktop/NGS/panels/reads/file1_OTHER.txt�: No such file or directory
Perhaps you might profit of programming a bit more defensive. Meaning, check your variables and value returns, etc, before assuming that it contains what you think.
Also, set -x in the beginning of your shell does display what is going on as it executes the program. Maybe even set -vx for verbosity.
Concerning your work flow I do not see a reason to not make something like this:
set -vx # for debugging only
SRC_DIR=/path/to/source_dir/
DEST_DIR=/path/to/destination_dir/ # notice the ending / and the upper case detonating that it is a constant that will not change in the live of the script.
# if you can not guarantee the correct permissions to access and modify the source and destination this might fail.
select filename in "$SRC_DIR"*; do
# The -v is to display what it is done
mv -v "$filename" "$DEST_DIR"
break
done
If you want the user to repeat the process of choosing, wrap the select block within a conditional loop. In the loop you must check by what criteria it must stop.
PS3="please select a file to analyze with entered gene or genes (or 1 or q to quit): "
select ANSW in exit /mnt/9/playground/*
do case $REPLY in 1|[eq]*) break;; esac
echo $ANSW "will be used to filter reads, identify target bases and genes less than 20 and 30 reads, create a low coverage bed for vizulization, and calcul
echo "Start selected file creation: Panel: ${FUNCNAME[0]} Date: $(date) - File: $ANSW"
file=${ANSW##*/}
echo mv -v "$ANSW" /mnt/9/playground/LA/${file%.jpg}_OTHER.txt ;
echo "End selected file creation: $(date) - File: $ANSW"
done | tee LOG