简体   繁体   English

bash输入输入并选择文件

[英]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 . ubuntu 14.04的下面bash提示用户输入,然后要求用户在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. 但是,我只能选择其中一个文件,因为尽管目录中有3个文件,但只有1个文件被标记。 However if I put the select file before the input the bash seems to work. 但是,如果我将select文件放在输入之前, bash似乎可以工作。 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. 的的修改IFS事先打破的方式select考虑您的目录列表:与IFS=,它现在正在寻找通过分离项目,同时您的ls的输出由线分开饲料。

You should restore your IFS to its previous value ( IFS=$OLDIFS ) before the select . select之前,应将IFS还原为其先前的值( IFS=$OLDIFS )。

Additionally, I would recommend using shell globing instead of a listing subshell's output : 另外,我建议使用外壳gloglo而不是列表子shell的输出:

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. 似乎您已将IFS值保存到OLDIFS,但随后没有还原它。 The expansion you use in the select command requires that IFS be restored to its default value. 在select命令中使用的扩展要求将IFS恢复为其默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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