linux - 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。
解决方案
您应该给外壳一个评估的机会。
V_DestFolder=`echo $1|awk -F"@" {'print $2'}`
for p in ${V_DestFolder}; do
if [ ! -w ${p} ]; then
推荐阅读
- java-8 - 使用 Java 8,我如何读取文件的内容,而我只能在文件中的任何位置获得冒号之后的内容?
- python - argparse subparser --help 输出不显示子解析器的描述
- mysql - 使用 Makefile 在 Docker 容器中运行 SQL 脚本
- amazon-web-services - 通过 SNS 和 Lambda 的电子邮件通知
- git - 使用 ARM 模板 C# 将私有 git 存储库部署到 Azure 应用服务时出错
- java - 如何将正则表达式匹配到字符和某些符号的任意组合,除了'% 和 %' 之间的一行中的两个破折号
- java - 如果由于 CMS gc 算法中的关键内存碎片而无法分配内存会发生什么
- neo4j - 将现有数据库导入 Neo4j 桌面应用程序 (v1.1.9)
- oauth - 加密的自定义字段是存储 Apex 标注的有限生命周期 OAuth 令牌的安全方式吗?
- jquery - 如何在jquery中将动态创建的字段设为必填字段