简体   繁体   English

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

[英]How to execute more than one command on one line?

I am new to Linux, and I am having an issue of not being able to fully execute these lines of code. 我是Linux的新手,并且遇到无法完全执行这些代码行的问题。 I have researched for hours and have also spent plenty of time manipulating these myself and still cannot create something that looks how it should be. 我已经研究了几个小时,也花了很多时间亲自操作这些东西,但仍然无法创建看起来应该是什么样的东西。 Thank you all ahead of time. 提前谢谢大家。

How it is supposed to look: 外观如何:

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

Here is how it currently looks. 这是目前的样子。

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

is is on the next line. is是在下一行。 This completely disregards my last line of command and does not display it on the same line as "the hostname for localhost.localdomain". 这完全无视我的命令的最后一行,并且与“ localhost.localdomain的主机名”不在同一行中显示。

Current file... 当前文件...

#!/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

Just interpolate the sub processes outputs: 只需内插子流程的输出:

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

The nested " are fine (the $(subshell) parsing takes precedence) 嵌套的"很好($(subshel​​l)解析优先)

You can use: 您可以使用:

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

Or, I suggest: 或者,我建议:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM