Bash to select and save file in new directory


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to select and save file in new directory
# 1  
Bash to select and save file in new directory

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 Smilie.

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"

# 2  
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 ?
# 3  
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 Smilie.

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 11:39 AM.. Reason: edited bash
# 4  
Quote:
Originally Posted by cmccabe
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 Smilie.

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.
# 5  
How about (ad[ao]pting Aia's proposal):

Code:
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


Last edited by RudiC; 06-19-2016 at 07:00 AM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #680
Difficulty: Easy
Solaris was written in the C and C++ languages.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Unset array element and save to file in Bash

#!/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)
Discussion started by: cogiz
5 Replies

2. Shell Programming and Scripting

Bash to select oldest folder in directory and write to log

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)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

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)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

Bash to select oldest folder in directory automatically and log process

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)
Discussion started by: cmccabe
0 Replies

5. Shell Programming and Scripting

Bash to select panel then specific file in directory

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)
Discussion started by: cmccabe
6 Replies

6. Homework & Coursework Questions

Save output into file bash scripting

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)
Discussion started by: shdin271
1 Replies

7. Shell Programming and Scripting

Save output into file bash scripting

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)
Discussion started by: shdin271
3 Replies

8. Shell Programming and Scripting

select data from oracle table and save the output as csv file

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)
Discussion started by: rdhanek
2 Replies

9. Shell Programming and Scripting

[bash] redirect (save) array to a file

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)
Discussion started by: ASGR
7 Replies

Featured Tech Videos