0

I am new to Linux, and I am having an issue of not being able to fully execute these lines of code. 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. This completely disregards my last line of command and does not display it on the same line as "the hostname for 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
1

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I have also added tput setaf 1 after 'Is:' to make the IP address turn red.
1

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"

1 Comment

Nice idea to show the use of a variable to get more readable code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.