如何在一行上执行多个命令?

时间:2015-10-11 20:01:01

标签: linux bash terminal

我是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: [kris@localhost 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

2 个答案:

答案 0 :(得分:2)

只需插入子流程输出:

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

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

答案 1 :(得分:1)

您可以使用:

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

或者,我建议:

numOfJudges
相关问题