首页 > 解决方案 > ksh脚本中变量路径中的变量部分

问题描述

如果过去已经回答过类似的问题,我很抱歉,但我找不到。我正在编写一个脚本来执行一些家务任务,但我陷入了以下步骤。为了让您记录下来,它是一个读取配置文件的脚本,以便能够在不同环境中将其用作标准协议。

问题在于这段代码:

# Check if destination folder exist, if not create it.
    if [ ! -d ${V_DestFolder} ]; then # Create folder
        F_Log "${IF_ROOT} mkdir -p ${V_DestFolder}"
        ${IF_ROOT} mkdir -p ${V_DestFolder}
        continue
    fi        

    # If movement, check write permissions of destination folder.
    V_CheckIfMovement=`echo $1|grep @`
    if [ $? -eq 0 ]; then # File will be moved.
      V_DestFolder=`echo $1|awk -F"@" {'print $2'}`
      if [ ! -w ${V_DestFolder} ]; then # Destination folder IS NOT writable.
        F_Log "Destination folder ${V_DestFolder} does not have WRITE permissions. Skipping."
        continue
      fi
    fi

基本上我需要(在此步骤中)将一些文件从一条路线移动到另一条路线。
它检查文件夹(从配置文件读取的名称)是否存在,如果不存在,将创建它,然后检查文件夹是否具有写权限并移动文件。

在这里您可以看到在此步骤中读取的配置文件部分:

app/tom*/instances/*/logs|+9|/.*\.gz)$/|move@/app/archive/tom*/logs

我需要说,当我将目标的 tom* 更改为任何内容时,文件已正确移动,例如“测试”或任何不带 * 的单词(应该如此)。

我需要知道的是如何在目的地的“tom *”中使用变量。变量应该在源代码中包含与 tom* 相同的名称,我将其用作单元格的名称。

这是因为我使用了不同的 tomcat 单元,参考 tom7 或 tom8 加上 3 个字母来描述每个单元格。例如 tom7dog 或 tom7cat。

标签: linuxbashvariablestomcatksh

解决方案


您应该给外壳一个评估的机会。

V_DestFolder=`echo $1|awk -F"@" {'print $2'}`
for p in ${V_DestFolder}; do
      if [ ! -w ${p} ]; then 

推荐阅读