【问题标题】:how to redirect the output file of a bash script to a desired folder如何将 bash 脚本的输出文件重定向到所需的文件夹
【发布时间】:2021-03-16 20:18:46
【问题描述】:

你好,我在这里运行这个 bash 脚本,命令“bwa index ..”会生成几个文件,这个命令没有用于指定输出路径的“-o”标志。我想将这些文件重定向到一个新文件夹中。我在网上搜索过,我遇到的所有答案都是将生成的文件重定向到特定的文件名,但是通常如何将输出文件重定向到新文件夹中?非常感谢!

#!/bin/bash

### qsub file.name to run from anywhere
### first step of BMW, making indexed fasta file

#PBS -N bwa_index
#PBS -S /bin/bash
#PBS -l walltime=24:00:00
#PBS -l nodes=1:ppn=8
#PBS -l mem=64gb
#PBS -o /gpfs/data/mcnerney-lab/liuweihan/chip_seq/becker_lab/bwa_index.out
#PBS -e /gpfs/data/mcnerney-lab/liuweihan/chip_seq/becker_lab/bwa_index.err

date

module load gcc/6.2.0
module load bwa/0.7.17

bwa index -p mm10_bwa_idx -a bwtsw /gpfs/data/mcnerney-lab/liuweihan/chip_seq/becker_lab/mm10.fa

date
echo END

【问题讨论】:

  • cd 目录 && bwa

标签: linux bash terminal


【解决方案1】:

您可以切换到目标目录,然后运行 ​​bwa 命令。使用pushd and popd 将确保脚本的其余部分在正确的目录中运行。

pushd some_directory
bwa index -p mm10_bwa_idx -a bwtsw /gpfs/data/mcnerney-lab/liuweihan/chip_seq/becker_lab/mm10.fa
popd

【讨论】:

    【解决方案2】:

    第一
    您可以定义一个变量并在您的脚本中使用它。

    第二
    您可以通过$1$2$3 等向脚本传递参数并捕获它们。

    第三如果您想通过使用选项(例如 --output | -o )实现完全动态,您可以使用如下简单的解析:

    #!/bin/bash
    
    ################################################################################
    # main flags, both longs and shorts
    ################################################################################
    ARGS=`getopt -o "o::" -l "output:" -- "$@"`
    eval set -- "$ARGS"
    
    
    declare -A _output;
    _output['flag']=0;
    _output['path']=0;
    
    ################################################################################
    # extract options and their arguments into variables.
    ################################################################################
    while true ; do
        case "$1" in
            -o | --output )
                _output['flag']=1;
                _output['path']=$2;
                shift 2;
            ;;
             --)
                shift;
                break;
             ;;
    
             * )
                echo "Internal error!" ; exit 1
             ;;
        esac
    done
    
    echo "output: ${_output['path']}";
    

    而且用法超级简单,可以使用--output <YOUR-PATH>或者-o <YOUR-PATH>

    # test
    ./cli.sh --output /path/to/dir
    output: /path/to/dir
    ./cli.sh -o /path/to/dir
    output: /path/to/dir
    

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      相关资源
      最近更新 更多