linux - 在 "while read"表达式中使用 sed

标签 linux bash sed while-loop

我对那个脚本很着迷。

#!/bin/bash

STARTDIR=$1
MNTDIR=/tmp/test/mnt

find $STARTDIR -type l  |
    while read file;
    do
       echo Found symlink file: $file
       DIR=`sed 's|/\w*$||'`
       MKDIR=${MNTDIR}${DIR}
       mkdir -p $MKDIR
       cp -L $file $MKDIR
    done

我将一些目录传递给 $1 参数,这个目录有三个符号链接(symbolic link)。在 while 语句中只回显了第一场比赛,在使用 sed 之后我输掉了所有其他比赛。 在下面查找输出:

[artyom@LBOX tmp]$ ls -lh /tmp/imp/
total 16K
lrwxrwxrwx 1 artyom adm   19 Aug  8 10:33 ok1 -> /tmp/imp/sym3/file1
lrwxrwxrwx 1 artyom adm   19 Aug  8 09:19 ok2 -> /tmp/imp/sym2/file2
lrwxrwxrwx 1 artyom adm   19 Aug  8 10:32 ok3 -> /tmp/imp/sym3/file3

[artyom@LBOX tmp]$ ./copy.sh /tmp/imp/
Found symlink file: /tmp/imp/ok1
[artyom@LBOX tmp]$ 

有人可以帮助解决这个问题吗? 谢谢

最佳答案

您忘记向 sed 提供一些东西。如果没有显式输入,它不会在此构造中读取任何内容。无论如何我都不会使用这种方法,而只是使用类似的方法:

DIR=`dirname "$file"`

关于linux - 在 "while read"表达式中使用 sed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6979063/

相关文章:

linux - 尝试启动 mongodb 时出错

linux - 在文件列表上运行命令

bash - .sh 脚本的第二行不执行

linux - 在 bash 中的行号文本后插入文本

python - 使用 DseAuthenticator 和 DseAuthorizer 从 Windows 将 python 连接到 cassandra 集群

c - 以编程方式执行递归 ping shell 命令

Linux 命令行帮助 |从文件中提取特定部分

linux - 删除具有给定模式的特殊字符的行

c++ - Winelib 能否将 DLL 直接链接到 ELF 可执行文件?

linux - 每秒显示运行 bash 脚本的运行时间