xargs 中的通配符变量用于并行处理

xargs 中的通配符变量用于并行处理

问题

当此函数位于 bash 调用中时,我在使用VAR函数(即)中的输入变量ls与 wilcard 时遇到问题。*xargs

xargs函数对文件名前缀列表的每个成员重复InfilePrefix_List.csv

输入变量VAR是以下形式的字符串:VAR="dir/sub_dir/SharedPrefix_InfilePrefix"

ls ${VAR}*给出错误,指出它无法找到该目录,VAR因为由于*.

解决方案

最后,输入列表被保存为 .csv 文件,并且 xargs 在每个列表条目后读取一个附加的“\r”。修改.csv文件后,功能运行顺利。在这里查看同样的问题:https://www.unix.com/unix-for-dummies-questions-and-answers/124289-xargs-appending-r.html

干杯

简化示例和并行计算

实际上,我使用的函数不是ls,而是:cdo ensmean infiles*.nc outfile.nc,这使得来自多个复制气候数据文件的集合均值。

但是,对于这篇文章,使用的示例ls infiles*.nc很好。请注意,我使用的xargs不是 for 循环替代方案,因为我需要利用xargs --max-procs 44选项提供的并行计算能力。

bash 脚本示例

#!/bin/bash
more /dir/subdir/File_Prefix_List.csv |  xargs -I {} bash -c "
echo 'arg is '{};
VAR=\$(echo /dir/subdir/SharedPrefix_{});
echo 'var is '\$VAR;
ls \${VAR}*.nc;
exit 1;"

笔记

第 2 行 - {} 代表特定InfilePrefix字符串

第 3,5 行 - 回显以检查 VAR 是否正确 - 它是......

第 6 行 - 示例功能是ls,列出SharedPrefix_InfilePrefix名称中包含的所有文件

调试编辑

根据要求,以下是经过此更改的示例 bash 脚本的输出:head -n 1 ...<my specific directories>... xargs -I {} bash -vxc " ...

请注意,对于我的具体情况,示例与我运行的代码之间的区别是: (1) /dir/subdir = /nobackup/b9035080/NCdata_RealsPalau。 (2) /SharedPrefix_ = /Reals_Palau_tos_Oday_. (3) {} = ACCESS-CM2_historical.

输出:第 1 行和第 2 行:xargs 参数 ( {} = ACCESS-CM2_historical) 和目录变量 ( VAR = /nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical) 正确。第3行:读取通配符时出错。

第 1 节输出的末尾显示了我认为相关的错误消息。

;cho 'arg is 'ACCESS-CM2_historical
' echo 'arg is ACCESS-CM2_historical
arg is ACCESS-CM2_historical
);R=$(echo /nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical
++ echo $'/nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical\r'
+ VAR=$'/nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical\r'
echo 'var is '$VAR;
' echo 'var is /nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical
var is /nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical
ls ${VAR}*.nc;
*.nc''/nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical
*.nc: No such file or directory35080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical
exit 1;
+ exit 1

第 2 部分 - 完整输出,其中一些我不明白 - 不确定它是否全部相关 - 抱歉 - 我是 Unix 新手!

module () {  eval $($LMOD_CMD bash "$@");
 [ $? = 0 ] && eval $(${LMOD_SETTARG_CMD:-:} -s sh)
}
ml () {  eval $($LMOD_DIR/ml_cmd "$@")
}
# -*- shell-script -*-
if [[ ${-/x} != $- ]]; then
   echo "Start of Lmod init/bash script to define the module command"
fi
+ [[ hvBc != hvxBc ]]
+ echo 'Start of Lmod init/bash script to define the module command'
Start of Lmod init/bash script to define the module command


LMOD_PKG=/usr/share/lmod/lmod
+ LMOD_PKG=/usr/share/lmod/lmod
LMOD_DIR=/usr/share/lmod/lmod/libexec
+ LMOD_DIR=/usr/share/lmod/lmod/libexec
LMOD_CMD=/usr/share/lmod/lmod/libexec/lmod
+ LMOD_CMD=/usr/share/lmod/lmod/libexec/lmod
MODULESHOME=/usr/share/lmod/lmod
+ MODULESHOME=/usr/share/lmod/lmod
export LMOD_PKG
+ export LMOD_PKG
export LMOD_CMD
+ export LMOD_CMD
export LMOD_DIR
+ export LMOD_DIR
export MODULESHOME
+ export MODULESHOME

########################################################################
#  Define the module command:  The first line runs the "lmod" command
#  to generate text:
#      export PATH="..."
#  then the "eval" converts the text into changes in the current shell.
#
#  The second command is the settarg command.  Normally LMOD_SETTARG_CMD
#  is undefined or is ":".  Either way the eval does nothing.  When the
#  settarg module is loaded, it defines LMOD_SETTARG_CMD.  The settarg
#  command knows how to read the ModuleTable that Lmod maintains and
#  generates a series of env. vars that describe the current state of
#  loaded modules.  So if one is on a x86_64 linux computer with gcc/4.7.2
#  and openmpi/1.6.3 loaded, then settarg will assign:
#
#     TARG=_x86_64_gcc-4.7.2_openmpi-1.6.3
#     TARG_COMPILER=gcc-4.7.2
#     TARG_COMPILER_FAMILY=gcc
#     TARG_MACH=x86_64
#     TARG_MPI=openmpi-1.6.3
#     TARG_MPI_FAMILY=openmpi
#     TARG_SUMMARY=x86_64_gcc-4.7.2_openmpi-1.6.3
#     TARG_TITLE_BAR=gcc-4.7.2 O-1.6.3
#     TARG_TITLE_BAR_PAREN=(gcc-4.7.2 O-1.6.3)
#
#  unloading openmpi/1.6.3 automatically changes these vars to be:
#
#     TARG=_x86_64_gcc-4.6.3
#     TARG_COMPILER=gcc-4.6.3
#     TARG_COMPILER_FAMILY=gcc
#     TARG_MACH=x86_64
#     TARG_SUMMARY=x86_64_gcc-4.6.3
#     TARG_TITLE_BAR=gcc-4.6.3
#     TARG_TITLE_BAR_PAREN=(gcc-4.6.3)
#
# See Lmod web site for more details.

module()
{
  eval $($LMOD_CMD bash "$@")
  [ $? = 0 ] && eval $(${LMOD_SETTARG_CMD:-:} -s sh)
}

LMOD_VERSION="6.5.1"
+ LMOD_VERSION=6.5.1
export LMOD_VERSION
+ export LMOD_VERSION

if [ "${LMOD_SETTARG_CMD:-:}" != ":" ]; then
  settarg () {
    eval $(${LMOD_SETTARG_CMD:-:} -s sh "$@" )
  }
fi
+ '[' : '!=' : ']'


########################################################################
#  ml is a shorthand tool for people who can't type moduel, err, module
#  It is also a combination command:
#     ml            -> module list
#     ml gcc        -> module load gcc
#     ml -gcc intel -> module unload gcc; module load intel
#  It does much more do: "ml --help" for more information.


unalias ml 2> /dev/null || true
+ unalias ml
+ true
ml()
{
  eval $($LMOD_DIR/ml_cmd "$@")
}

export_module=$(echo "YES" | /usr/bin/tr '[:upper:]' '[:lower:]')
++ echo YES
++ /usr/bin/tr '[:upper:]' '[:lower:]'
+ export_module=yes
if [ -n "${BASH_VERSION:-}" -a "$export_module" != no ]; then
  export -f module
  export -f ml
fi
+ '[' -n '4.2.46(2)-release' -a yes '!=' no ']'
+ export -f module
+ export -f ml
unset export_module
+ unset export_module

########################################################################
#  clearMT removes the ModuleTable from your environment.  It is rarely
#  needed but it useful sometimes.

clearMT()
{
  eval $($LMOD_DIR/clearMT_cmd bash)
}

########################################################################
#  The following make the action of the settarg available to the titlebar
#  for both xterm's and screen but only for interactive shells.
if [ -n "${PS1:-}" ]; then
  if [ -n "${LMOD_FULL_SETTARG_SUPPORT:-}" -a "$LMOD_FULL_SETTARG_SUPPORT" != no ]; then
    xSetTitleLmod()
    {
      builtin echo -n -e "\033]2;$1\007";
    }
    SET_TITLE_BAR=:

    case "$TERM" in
      xterm*)
        SET_TITLE_BAR=xSetTitleLmod
        ;;
    esac

    SHOST=${SHOST-${HOSTNAME%%.*}}
    precmd()
    {
      eval $(${LMOD_SETTARG_CMD:-:} -s bash)
      ${SET_TITLE_BAR:-:} "${TARG_TITLE_BAR_PAREN}${USER}@${SHOST}:${PWD/#$HOME/~}"
      ${USER_PROMPT_CMD:-:}
    }

    # define the PROMPT_COMMAND to be precmd iff it isn't defined already.
    : ${PROMPT_COMMAND:=precmd}
  fi
fi
+ '[' -n '' ']'

########################################################################
#  Make tab completions available to bash users.

if [ ${BASH_VERSINFO:-0} -ge 3 ] && [ -r  /usr/share/lmod/lmod/init/lmod_bash_completions ] && [ -n "${PS1:-}" ]; then
 . /usr/share/lmod/lmod/init/lmod_bash_completions
fi
+ '[' 4 -ge 3 ']'
+ '[' -r /usr/share/lmod/lmod/init/lmod_bash_completions ']'
+ '[' -n '' ']'

if [[ ${-/x} != $- ]]; then
   echo "End of Lmod init/bash script to define the module command"
fi
+ [[ hvBc != hvxBc ]]
+ echo 'End of Lmod init/bash script to define the module command'
End of Lmod init/bash script to define the module command

;cho 'arg is 'ACCESS-CM2_historical
' echo 'arg is ACCESS-CM2_historical
arg is ACCESS-CM2_historical
);R=$(echo /nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical
++ echo $'/nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical\r'
+ VAR=$'/nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical\r'
echo 'var is '$VAR;
' echo 'var is /nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical
var is /nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical
ls ${VAR}*.nc;
*.nc''/nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical
*.nc: No such file or directory35080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical
exit 1;
+ exit 1

答案1

bash -vx正如您在输出行中看到的

+ VAR=$'/nobackup/b9035080/NCdata_RealsPalau/Reals_Palau_tos_Oday_ACCESS-CM2_historical\r'

\r输入行末尾有一个回车符 (CR, )。除非 Unix 换行符 (LF, \n) 未被剥离并成为字符串的一部分,当然,它不再与文件系统路径匹配。

相关内容