我已经看到这对StackOverflow问了很多,但我似乎无法找到适合我的解决方案。
问题 我有一个for循环根据用户输入的内容生成每个ipaddress的ipaddresses和名称列表。因此,系统会询问用户"您想要将该系列称为什么,第一个ipaddress的数量是多少?有多少个?"。
因此,如果用户输入slave,12(在这种情况下将0附加到末尾,然后是3),然后是3.然后我希望sed将此列表放入/ etc / hosts中。
我尝试了什么 我正在使用perl,我尝试使用perl one liner从之前插入(之前我曾使用过XML文档),但它没有用。我尝试了sed和(我认为)每个ipaddress和名称之间的空白区域正在抛弃它。我试过谷歌搜索,但我尝试过的解决方案没有用。要么我在编写代码时搞砸了,要么我有一个特例。对我来说不幸的是,我认为它是前者而不是后者的情况!但是我不知道我哪里出错了!
守则
printf "\nMake sure you have ran part 1 of this installation script\nThis is the script for a single node instance\n"
function begin () {
getmasterip
}
function getmasterip () {
#printf "\nNow getting ipaddress of master node\n"
ip="$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')"
echo $ip
read -p "Enter slave series name: " series
read -p "Enter number of slaves: " number
read -p "Enter ip address of first slave (192.168.160.XX0): " ipadd
#echo "192.168.160.${ipadd}0 is the first slave, will create series now"
arr=""
#arr=('${ip} hdmaster')
sn="$number"
#for ((i=0; i<sn; ++i )); do
#arr+="192.168.160.${ipadd}${i} ${series}${i}\n"
#done
insert=$(for ((i=0; i<sn; ++i)); do
printf "192.168.160.${ipadd}${i} ${series}${i}"
done )
printf "\nNow adding ipaddress to /etc/hosts storing in output for test\n"
#sudo cp /etc/hosts /etc/hosts.bak
#perl -ne 'print $_;print "\n'${ip}' hdmaster\n" if(/ubuntu/);' /etc/hosts > /etc/hosts
find="ubuntu"
line="${ip} hadmaster\n ${arr}"
#sed -i "${line} i \ \ ${text}" /etc/hosts
echo "line to find --- " ${find}
echo "text to insert --- " ${insert}
echo "hdmaster --- " ${ip}
sed -i '/ubuntu/a ${insert} ' /etc/hosts
}
function cases () {
read -p "WARNING: This script is intended to be RUN AS SUDO and as part 2, would you like to continue? (y/n): " choice
case $choice in
y)
begin;;
n)
echo "Terminating program"
exit 1;;
*)
echo "Use either y (yes) or n (no)"
cases;;
esac
}
cases
我有更多注释掉的代码,但我只删除了那些我完全确定在完全错误的轨道上的代码。我在网站上看到了这篇文章(sed insert line with spaces to a specific line),并认为我可以将整个地段存储在一个字符串中,其中包含新编码的字符串。白色空格仍然插入插入thogh(我认为这是问题)
更多信息 我想改进我的bash脚本和整体Linux技能,所以我想我会在VM上为Hadoop集群创建一个安装脚本作为练习方式。
TL:DR 我试图插入一堆带有sed的字符串(由for循环生成)。我怎么能这样做,因为我似乎无法掌握sed。
谢谢你,:) 博斯卡
修改 对不起,我遗漏了错误代码我得到了!
sed: can't read slave0192.168.160.121: No such file or directory
sed: can't read slave1192.168.160.122: No such file or directory
sed: can't read slave2 : No such file or directory