我是一个 n00b,试图将我的 IP 地址返回给一个变量,然后在 bash 脚本中的 sed 命令中使用该变量。我正在尝试用我的 IP 地址替换文件中的文本“mycomputer”,但运气不佳。
这是我的尝试:
1
localip=`ipconfig getifaddr en0`
sed -i '' “s/mycomputer/$localip/” config.txt
我收到的错误是:
sed: 1: "“s/mycomputer/192.168 ...": invalid command code ?
2
localip=`ipconfig getifaddr en0`
sed -i '' 's/mycomputer/$localip/g' config.txt
将 'mycomputer' 更改为 '$localip' - 不是实际的 IP 地址
3
localip=`ipconfig getifaddr en0`
sed -i '' 's/mycomputer/‘“$localip”’/g’ config.txt
错误:
./mytest.sh: line 5: unexpected EOF while looking for matching `''
./mytest.sh: line 6: syntax error: unexpected end of file
有什么想法吗?!?!
编辑:
这用于 bash 脚本,如下所示:
#!/bin/bash
cd "`dirname "$0"`"
localip=`ipconfig getifaddr en0’
sed -i '' "s/mycomputer/$localip/" config.txt