我正在尝试根据我的/ etc / hosts为我的每台服务器创建.remmina配置文件,我编写了下一个脚本,但我的问题是在sed命令的结果中,我期望在哪里看到$hostname
变量的值而不是我看到单词$ hostname,它怎么能正确完成?
#!/bin/bash
num=100
for srv in `cat ~/srv.lst`;
do cp 1.remmina $num.remmina
hostname=grep $srv /etc/hosts | awk -F" " '{print $2}'
sed -i 's/name=amavm4/name=$hostname/g' ~/.remmina/$num.remmina
sed -i 's/server=amavm4:5906/server=$hostname:5906/g' ~/.remmina/$num.remmina
num=$((num+1))
done
所以在你的答案之后,我编辑了这样的脚本:
#!/bin/bash
num=1392344753668
for srv in `cat ~/srv.lst`;
do cp 1.remmina $num.remmina
hostname=`grep $srv /etc/hosts | awk -F" " '{print $2}'`
sed -i 's/name=amadw1/name='$hostname'/g' ~/.remmina/$num.remmina
sed -i 's/server=amadw1:5906/server='$hostname':5906/g' ~/.remmina/$num.remmina
num=$((num+1))
done
但是当我运行它时,我得到以下错误:
itaig@itaig-lt:~/.remmina$ ./rem_add_srv
sed: -e expression #1, char 26: unterminated `s' command
sed: -e expression #1, char 35: unterminated `s' command
sed: -e expression #1, char 26: unterminated `s' command
sed: -e expression #1, char 35: unterminated `s' command
sed: -e expression #1, char 31: unterminated `s' command
sed: -e expression #1, char 40: unterminated `s' command
sed: -e expression #1, char 25: unterminated `s' command
sed: -e expression #1, char 34: unterminated `s' command
sed: -e expression #1, char 22: unterminated `s' command
sed: -e expression #1, char 31: unterminated `s' command
sed: -e expression #1, char 26: unterminated `s' command
sed: -e expression #1, char 35: unterminated `s' command
sed: -e expression #1, char 26: unterminated `s' command
sed: -e expression #1, char 35: unterminated `s' command
sed: -e expression #1, char 26: unterminated `s' command
sed: -e expression #1, char 35: unterminated `s' command
sed: -e expression #1, char 28: unterminated `s' command
sed: -e expression #1, char 37: unterminated `s' command
sed: -e expression #1, char 24: unterminated `s' command
sed: -e expression #1, char 33: unterminated `s' command
sed: -e expression #1, char 26: unterminated `s' command
sed: -e expression #1, char 35: unterminated `s' command
sed: -e expression #1, char 25: unterminated `s' command
sed: -e expression #1, char 34: unterminated `s' command
itaig@itaig-lt:~/.remmina$
在脚本创建多达87个文件时,我似乎只得到20个错误,为什么会出现此错误?
答案 0 :(得分:0)
您需要确保两件事:
shell变量需要由shell评估;因此必须用双引号括起来,而不是用单引号括起来。另外,不鼓励在引号之外使用它(当前版本),因为值中的字符由shell计算,诸如空格之类的东西变成令牌分隔符等。所以你应该使用这个版本:
sed -i“s / name = amadw1 / name = $ hostname / g”〜/ .remmina /“$ num”.remmina
变量的值不得包含换行符,因为sed
只能处理其s
命令中的单行值。我假设您的grep
命令生成多行,awk
更改每行,因此结果仍然是多行。要避免这种情况,请考虑要使用多行中的哪一行。也许是第一个?然后使用head -1
:
主机名= grep $srv /etc/hosts | head -1 | awk -F" " '{print $2}'
答案 1 :(得分:0)
#!/bin/bash
num=1392344753668
cat ~/srv.lst | while read srv
do
cp 1.remmina ${num}.remmina
sed -n "/${srv}/ s/^[^[:blank:]]\{1,\}[[:blank:]]\{1,\}\([^[:blank:]]\{1,\}\).*/\1/p;s/$/ void/" /etc/hosts | read hostname Ignore
if [ -n "${Ignore}" ]
then
sed -i "s/name=amadw1/name=${hostname}/g" ~/.remmina/$num.remmina
sed -i "s/server=amadw1:5906/server=${hostname}:5906/g" ~/.remmina/$num.remmina
let num+=1
else
echo "Bad hostname for srv: ${srv}" >&2
fi
done
在KSH工作,认为它在bash中也可以。 只是确保主机名不包含“&” “\ 1” - > “\ 9”(通常不是)