简体   繁体   中英

bash to enter input and select file

The below bash in ubuntu 14.04 prompts the user for input then after that asks for the file to use in a select . However, I can only select one of the files as there is only 1 that is marked though there are three in the directory. However if I put the select file before the input the bash seems to work. I can not seem to fix it, what is wrong? Thanks.

Bash

# enter gene input
printf "%s \n" "Please enter gene(s), use a comma between multiple:"
OLDIFS=$IFS
IFS=","
read -a genes
for (( i = 0; i < ${#genes[@]}; i++ ))
do
printf "%s\n" "${genes[$i]}" >> /home/cmccabe/Desktop/NGS/panels/GENE.bed
done

# 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}
 echo "End selected file creation: $(date) - File: $file"
 done >> "$logfile"

Displayed in terminal

1) 12_newheader_base_counts.txt
   34_newheader_base_counts.txt
   56_newheader_base_counts.txt

Desired terminal display

1) 12_newheader_base_counts.txt
2) 34_newheader_base_counts.txt
3) 56_newheader_base_counts.txt

The modification of the IFS beforehand breaks the way the select considers your directory listing : with IFS=, , it's now looking for item separated by , , while your ls 's output is separated by line feeds.

You should restore your IFS to its previous value ( IFS=$OLDIFS ) before the select .

Additionally, I would recommend using shell globing instead of a listing subshell's output :

select file in /home/cmccabe/Desktop/NGS/API/test/bedtools/*;

Seems like you saved the IFS value to OLDIFS but then you didn't restore it. The expansion you use in the select command requires that IFS be restored to its default value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM