0

I'm trying to edit a simple string in sed that I've been having issues with. Basically what I'm trying to do is grab my current working directory using pwd, pipe it to sed, edit the file path, then check the file paths contents using ls. The reason I need to do this is to dynamically check backups to see if backups contain the files I need in the folder that I'm currently working in (just going to make it into a quick hotkey).

Here's what I've got so far:

    pwd | sed "s/`whoami`/`whoami`.{daily,weekly,monthly}/g" | xargs ls

When running this on my server (it's a shared hosting server, hence the need for whoami instead of just username) I get this error:

    /bin/ls: cannot access /home1/username.{daily,weekly,monthly}: No such file or directory

However, I'm able to run this command without any issues:

    ls /home1/username.{daily,weekly,monthly}

I've tried probably ~20 different variations of this command (various sed and xargs flags as well) and can't seem to get it to work properly. I believe this error is being caused by how sed handles the search and replace with streams, however I don't know how to get around it or what exactly it is that's causing it.

I believe it's sed that's causing the issue as this command has the same error:

    ls `pwd | sed "s/$(whoami)/$(whoami).{daily,weekly,monthly}/g"`
    /bin/ls: cannot access /home1/username.{daily,weekly,monthly}: No such file or directory

If anyone can tell me why it's happening, or some way for sed to properly pass the edited version to ls. I should be able to figure out a command for it, I just need to know why. Thanks!

4

2 に答える 2

2

{daily,weekly,monthly}文字列は によって解釈されますが、bash はこのbash文字列を認識しません。これは と から直接渡されsedます。xargsls

文字列を拡張する機会を与えるにbashは、次のことを試してください。

eval ls $(pwd | sed "s/`whoami`/`whoami`.{daily,weekly,monthly}/g")
于 2013-02-19T02:18:12.050 に答える
0

bash中括弧を展開するので、

ls /home1/username.{daily,weekly,monthly}

実際には次のように実行されます

ls /home1/username.daily /home1/username.weekly /home1/username.monthly

という名前のディレクトリ/home1/username.{daily,weekly,monthly}は存在しません。ブレース展開xargsをバイパスして、文字通り受け取ったものをそのまま渡します。bash

これを参照してください:

echo a{b,c}d
>>> abd acd

echo "a{b,c}d" | xargs echo
>>> a{b,c}d

したがって、あなたの問題はsed、でもありませんls。拡張を手動で行う方法を見つける必要があります。最も簡単な方法は使用することですeval(編集:エイドリアン・プロンクが示すように)

于 2013-02-19T02:13:12.240 に答える