linux - 使用字符串变量更改 shell 脚本中的目录

标签 linux bash shell command-line scripting

我有一系列目录,它们只是数字标签不同。

arr=(0 1 2 3)
i=0
while [ $i -le ${arr}]
do
  dir="~Documents/seed" 
  dir+=${arr[i]}
  echo $dir #works
  cd dir #directory not found
  #do other things#
done

这有可能吗?

最佳答案

这可能更容易:

#!/bin/bash
for d in ~/Dcouments/seed*
do
   if [ -d "$d" ]; then
      echo $d
   fi
done

注意:

您在 ~/Documents 中也有 tarfile(名称也与通配符匹配),所以我添加了一个 if 语句来检查它是目录还是文件并且只对目录使用react.

关于linux - 使用字符串变量更改 shell 脚本中的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24252680/

相关文章:

c - 我怎样才能在pam中获取客户端ip

linux - 加载共享库时出现 cscope 错误

python - 使用 Python 输出调用程序

bash - 本地机器覆盖的 Docker 镜像环境变量

node.js - 从作为子进程运行的脚本将 Node 作为后台进程运行

linux -/proc/self 和/proc/$$ 之间有什么区别?

linux - Bash 间接变量引用

sql - 如何在外部表中设置参数?

python - subprocess.call() 如何与 shell=False 一起工作?

linux - 在 Unix 上计算每行/字段的字符出现次数