【问题标题】:working with arrays in bash and do stuff在 bash 中处理数组并做一些事情
【发布时间】:2014-05-27 04:24:14
【问题描述】:

我正在编写一个 bash 脚本并且需要帮助。这是我尝试过的:

在@merlin2011 的帮助下

#!/bin/bash

if [ $# -ne 2 ]; then
    echo "Usage: `basename $0` <absolute-path> <number>"
    exit 1
fi

if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

#find . -name "$2" -exec mv {} /some/path/here \;
find $1 >> /tmp/test
for line in $(cat `/tmp/test`); do
    echo $line | mv $2 awk -F"/" '{for (i = 1; i < NF; i++) if ($i == "$2") print $(i-1)}'
done

现在我想从数组中检查find 命令的结果,然后如果有一个名为2010 的目录,然后获取它的绝对路径。例如:

arr[1]="/path/to/2010/file.db"

然后我想将2010 重命名为父目录to。我的模式是:

arr[1]="/path/to/2010/file.db"
arr[2]="/path/test/2010/fileee.db"
arr[3]="/path/tt/2010/fileeeee.db"
.
.
.
arr[100]="/path/last/2010/fileeeeeee.db"

结果应该是:

mv /path/to/2010/ to
mv /path/test/2010 test
mv /path/tt/2010/ tt
.
.
.
mv /path/last/2010 last

更新:

我完全想知道如何在awk 中反向获取变量...

/path/to/dir1/2010/file.db

我想在 absolute path 中搜索,然后找到 2010 并在以前的路径中将其重命名为 / pattern like : awk -F"/" {print [what?]}

告诉 awk 我的状态是 2010 年,然后通过知道 splitter 是 / 在它之前打印一个变量

更新

文件目录和子目录模式是:

/path/to/file/efsef/2010/1.db
/path/to/file/hfjh/sdfsf/2010/2.db
/path/to/file/dsf/sdhher/aqwe/sfrt/2010/3.db
.
.
.
/path/to/file/kldf/2010/100.db

我想将所有 2010 年的目录重命名为它们的父级然后 tar all .db

这正是我想要的:)

【问题讨论】:

  • 我刚刚更新了。让我知道这是否符合您的要求。
  • 所以您想将/path/to/dir1/2010/file.db 传递给awk 并搜索2010?如果找到2010,接下来你想做什么?
  • 谢谢...如果找到2010 [第二个参数]我想将其重命名为父目录...
  • 所以你想要:mv /path/to/dir1/2010/file.db dir1?
  • 我想做:mv $2 [$2的父目录]

标签: arrays bash awk


【解决方案1】:
        To implement, we have to do the recursive folder search.

        It should be combination of two commands like find and mv




        find . -name "2010" -exec mv {} /some/path/here \;


Other way shared by merlin2011
    mv $2 awk -F"/" `'{for (i = 1; i < NF; i++) if ($i == "$2") print $(i-1)}'`

【讨论】:

  • 这应该是:find $1 -name "$2" -exec mv {} [Here should be the parent directory of 2010] \;
  • 好吧...我想用awk找父目录...我不擅长awk
【解决方案2】:

此答案仅针对 OP 的更新。我最好的解释是您试图让awk 在字符串/path/to/dir1/2010/file.db 中打印值dir1。以下行将实现它。

awk -F"/" '{for (i = 1; i < NF; i++) if ($i == "2010") print $(i-1)}'

我用下面的命令测试过,会输出dir1

echo /path/to/dir1/2010/file.db | awk -F"/" '{for (i = 1; i < NF; i++) if ($i == "2010") print $(i-1)}'

根据您的更新,您应该使用backtic 运算符将awk 命令括起来。

mv $2 `awk -F"/" '{for (i = 1; i < NF; i++) if ($i == "$2") print $(i-1)}'`

【讨论】:

  • 谢谢..非常好..但是:如何在这里告诉 rename 2010 到 awk 的结果?
  • 我将根据您的数组正确的假设进行更新。
  • 我喜欢你的解决方案。一路走好@merlin2011
  • 在您的帮助下:它 THIS 为我工作?
  • @MortezaLSC,在我更新的答案中使用最后一行。请记住,我假设echo $line 中始终包含2010。如果不是这样,那么mv 命令将简单地抛出错误,因为它只有一个参数。
【解决方案3】:

这里是 awk 命令:

awk -F"/$2/" '{split($1, a, "/"); system("echo mv " $0 " " a[length(a)]);}' <<< "$1"
mv /path/to/dir1/2010/file.db dir1

一旦您满意,请在系统命令中删除 echo

【讨论】:

  • 请查看this
  • 这就是它的作用。您可以设置s="$2"dir1 不是 2010 的父目录吗?
  • 是的,但这本身就是一个变量...可能是 dir1 也可能是其他任何东西...所以我认为命令 mv /path/to/dir1/2010/file.db dir1 不应该工作
  • 是的,它可以是任何东西。在上面的命令中如果s='/path/to/anything/2010/file.db' 那么输出将是:mv /path/to/anything/2010/file.db anything 你还期待什么?
  • 好的...你能把整个脚本放上去吗?由于一些问题,我无法编写它...非常感谢您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 2017-08-24
相关资源
最近更新 更多