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
Code:
# 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
Code:
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:
Code:
# 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
Code:
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
Last edited by cmccabe; 06-18-2016 at 10:39 AM..
Reason: edited bash
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
Code:
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:
Code:
# 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
Code:
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:
Code:
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
#!/bin/bash
X=(2H 4S 10D QC JD 9H 8S)
How do I unset the 10D from this array and save it to a file?
Please use CODE tags as required by forum rules! (5 Replies)
In the bash below the oldest folder in a directory is selected. If there are 3folders in the directory /home/cmccabe/Desktop/NGS/test and nothing is done to them (ie. no files deleted, renamed) then the bash correctly identifies f1 as the oldest. However, if something is done to the folder then... (4 Replies)
In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :)
... (4 Replies)
The `bash` below uses the oldest folder in the specified directory and logs it. The goes though an analysis process and creates a log. My problem is that if there are 3 folders in the directory folder1,folder2,folder3, the bash is using folder2 for the analysis eventhough folder1 is the oldest... (0 Replies)
I am using bash to prompt a user for a choice using: where a "y" response opens a menu with available panels that can be used.
while true; do
read -p "Do you want to get coverage of a specific panel?" yn
case $yn in
* ) menu; break;;
* ) exit;;
* ) echo... (6 Replies)
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
Hi there.
i have created a program that in the end it will give output like this
1 2 3 4 5
10 9 ... (1 Reply)
Hi there.
i have created a program that in the end it will give output like this
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
.............. 17
i wonder how to save the output into a single string and into a file.
i.e 1 10 11 12 9 2 3 8 13 14 7 4 5 6 15 17 (in this order,... (3 Replies)
Hi
I need to execute a select statement in a solaris environment with oracle database. The select statement returns number of rows of data.
I need the data to be inserted into a CSV file with proper format. For that we normally use "You have to select all your columns as one big string,... (2 Replies)
Hi,
I'm trying to write a function that redirects the contents of an
array to a file. The array contains the lines of a data file with
white space.
The function seems to preserve all white space when redirected
except that it seems to ignore newlines. As a consequence, the
elements of the... (7 Replies)