2015-10-11 34 views
0

我是Linux新手,我遇到了無法完全執行這些代碼行的問題。我已經研究了好幾個小時,並且花了很多時間來操縱這些,但仍然無法創造出看起來應該如何的東西。提前謝謝大家。如何在一行上執行多個命令?

如何它應該看看:

$. network.sh 
Today is: Day, Month, Day (numerical), year 
The hostname for localhost.localdomain is: 192.168.xxx.xxx 

這是怎麼回事目前外觀。

. network.sh 
Today is: Sunday, October 11, 2015 
The hostname for localhost.localdomain 
is: [[email protected] Desktop]$ 

is是在下一行。這完全忽略了我的最後一行命令,並沒有將它顯示在與「localhost.localdomain的主機名」相同的行上。

當前文件...

#!/bin/bash 
# Script to print the current date 
echo -n "Today is: " 
d=$(date +%A," "%B" "%d," "%Y) 
echo "$d" 
# Show IP in color 
echo -n "The hostname for " ; hostname ; echo -n "is:" ; echo -n ip addr list eth1 |grep "inet " |cut -d' ' -f6|cut -d/ -f1 
+0

看http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html – sehe

回答

2

只需插子流程輸出:

echo "The hostname for $(hostname) is: $(ip addr list eth1 |grep "inet " |cut -d' ' -f6|cut -d/ -f1)" 

嵌套"都很好($的(子shell)解析優先)

+0

太謝謝你了!我在'Is:'後面添加了'輸入setaf 1',使IP地址變爲紅色。 – Kris

1

你可以使用:

echo "The hostname for $(hostname) is: $(ip addr list eth1 | grep "inet " | cut -d' ' -f6 | cut -d/ -f1)" 

或者,我建議:

ip=$(ip addr list eth1 | grep "inet " | cut -d' ' -f6 | cut -d/ -f1) 
echo "The hostname for $(hostname) is: $ip" 
+0

好主意,以顯示使用變量來獲得更多可讀代碼 – sehe