2014-12-06 27 views
1

我試圖讓一個腳本,將幾乎自動完成這一過程意外結束: http://knowledgelayer.softlayer.com/procedure/add-additional-ips-redhat嘗試了我的第一個bash腳本,不斷收到文件

不太知道它會如何工作,但沒在我能將腳本寫入一個腳本之前,我們不會太過分。下面是腳本的內容:

編輯與更新的代碼: 編輯#2:知道了大部分的工作,但是現在它運行循環,跳過讀取propmt以獲取靜態IP

#!/bin/bash 

path=/etc/sysconfig/network-scripts/ 
echo "Let's get your IP added :)" 
echo"" 


getnewip() 
{ 
echo read -p "Enter the new IP Address you wish to add: " staticip 
} 

getserverinfo() 
{ 
gateway=$(netstat -rn | sed -n 3p | awk '{print $2}') 
netmask=$(ifconfig eth1 | grep -i 'netmask' | grep -v '127.0.0.1' | awk '{print $4}') 
clone=$(ifconfig | grep eth1 | awk '{print $1}' | cut -d: -f2) 
} 


rangechecks() 
{ 
cd /etc/sysconfig/network-scripts/ 
ls ifcfg-eth1-range* 

filename==$1 
if [[ ! -f $filename ]]; then 
touch "$filename" 
echo "Created \"$filename\"" 

fi 

digit=1 
while true; do 
temp_name=$filename-$digit 
if [[ ! -f temp_name ]]; then 
touch "$temp_name" 
echo "Created $path\"$temp_name\"" 
digit=$((digit + 1)) 
fi 
done 
} 


writeinterfacefile() 
{ 
cat >> "/etc/sysconfig/network-scripts/$1" << EOF 
IPADDR_START=$staticip 
IPADDR_END=$staticip 
NETMASK=$netmask 
CLONENUM_START=$((clone+1)) 
EOF 

echo "" 
echo "Your information was saved in the file '$1'." 
echo "" 


} 
{ 
clear 
getserverinfo 
echo "" 
echo "Please verify this information: " 
echo "Gateway Address: " "$gateway" 
echo "Netmask: " "$netmask" 
echo "Your new IP: " "$staticip" 
echo '' 

while true; do 
read -p "Is this information correct? [y/N]" yn 
case $yn in 
[Yy]*) $writeinterfacefile;; 
[Nn]*) print "$getserverinfo" && exit ;; 
*) echo 'Please enter Y or n';; 
esac 
done 
} 

我在腳本編程方面相當新穎,所以請原諒可怕的語法。我的眼睛在EOF上,但我不知道。

+0

請看看http://www.shellcheck.net/ – Cyrus 2014-12-06 13:28:21

+0

實際上發現,現在所有的錯誤都消失了,但仍然得到了不希望的結果。 – 2014-12-07 05:57:27

+0

儘量不要使用'echo「」'或'echo'在輸出中插入空行。使用'echo -e'\ nWhatever .. \ n「'或'printf」\ n無論如何\ n \ n「''echo'的'-e'選項可以解釋轉義。 'printf'總是解釋轉義。如果您使用帶有語法突出顯示功能的編輯器並正確地縮進您的代碼,那麼您的腳本(查找意外的EOF等)將大大受益。在Linux上''konsole/gedit'是固定高亮的gui編輯器,'vim/emacs'是很棒的基於文本的編輯器。在Windows方面,'notepad ++'有很好的突出顯示,並且是'GPL'。 – 2014-12-07 08:29:20

回答

0

我看不到的功能rangechecks

而且右大括號,不縮進家當在第一線。

+0

添加一個後,我得到以下內容: addips.sh:第38行:意外標記附近的語法錯誤'}' addips.sh:第38行:'}' – 2014-12-06 13:29:11

1

rangechecks沒有},你while沒有done ...
你應該縮進代碼。我開始這樣做,馬上注意到了這個錯誤。

其他的事情:

  • 單引號不擴大變量,'/etc/sysconfig/network-scripts//$1不會做你希望它是什麼。
  • echo ""相當於echo
  • foo='bar'; echo "blah" echo -n $foo會輸出'blah echo -n bar'。
  • exit退出腳本,我不確定那是您的看法。
  • [y/N]默認情況下表示爲N(大寫字母)。
    此外,您然後要求enter Y or n。始終如一!
  • 將變量用作參數時,請雙引號。這可以確保它保持原樣(作爲一個參數,而不是由shell擴展)。
+0

更新了主帖子中的代碼。至於y/N部分,我只需要這樣做一次?!其餘的我想我已經修好了。空的回聲是空間,不知道爲什麼我在那個習慣大聲笑。現在我得到addips.sh:第36行:語法錯誤附近的意外令牌'完成' – 2014-12-06 13:45:51

+0

修復它,我的fi是在我完成>。> Lmao後。腳本現在可以工作,謝謝老闆! – 2014-12-06 13:48:00

相關問題