有人告诉我有可能,但是没有一个单独的示例,它sed
可以从字符串变量读取而无需输入文件。我还没有开始工作。为了安全起见,我将$PATH
变量写到另一个变量中,因为我对此一无所知,因为在我完全不知道该怎么做之前,我不需要其他问题。
考虑以下:
~$x=$PATH
~$sed -i 's/:/ /g' $x
失败的原因是:没有这样的文件或目录。
这是我尝试过的一些其他方法:
~$ sed -i 's/:/ /g' | (read x)
sed: no input files
~$ sed -i 's/:/ /g' | (read $x)
bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier
sed: no input files
~$ sed -i 's/:/ /g' | $(read x)
sed: no input files
~$ sed -i 's/:/ /g' | $(read $x)
bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier
sed: no input files
~$ sed -i 's/:/ /g' < $(read $x)
bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier
bash: $(read $x): ambiguous redirect
~$ sed -i 's/:/ /g' < read $x
bash: read: No such file or directory
~$ sed -i 's/:/ /g' < (read x)
bash: syntax error near unexpected token `('
~$ sed -i 's/:/ /g' < $((read x))
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: syntax error: operand expected (error token is "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games")
~$ sed -i 's/:/ /g' < $((read $x))
bash: read /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: division by 0 (error token is "usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games")
~$ sed -i 's/:/ /g' << $((read $x))
> ;^C
~$ sed -i 's/:/ /g' << $(read $x)
> ;^C
~$ sed -i 's/:/ /g' $x
sed: can't read /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory
~$ sed -i 's/:/ /g' < $x
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory
~$ sed -i 's/:/ /g' < echo $x
bash: echo: No such file or directory
~$ sed -i 's/:/ /g' | echo $x
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
sed: no input files
这可以工作吗?我宁愿不需要编写不需要的文件,也可以使用sed。对于这个特定的示例,如果~$x=$PATH ~$sed -i 's/:/ /g' $x
按照我希望的方式实际工作,我将得到:
/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games /usr/local/games
然后我可以将其分配给变量,并在以后的命令中使用,例如 ls $x
x=$(echo "$x" | sed 's/:/ /g')
?