Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bash Onliner ... 1

Status
Not open for further replies.

lslamp

IS-IT--Management
Jun 19, 2008
2
NL
Greetings All,

I hope that someone can help me here. I have the following script where I would like to have the output on a single line.

below is my script that I would hope to do.
# # # # # # # # #
#!/bin/bash
#set -x

STARTHOME=$(date +%s)
sleep 60
ENDHOME=$(date +%s)

echo "HOME Time :" $((ENDHOME-STARTHOME)) | awk '{print int($1/60)":"int($1%60)}'
echo ""
echo "# This should appear in two lines"
echo "HOME Time :"
echo $((ENDHOME-STARTHOME)) | awk '{print int($1/60)":"int($1%60)}'

# # # # # # # # #

Below is what I see in the output.

The output that I would like to see is as follows.

llamprec@ICANWeb:~$ ./test.sh
# This should appear in a single line.
0:0

# This should appear in two lines
HOME Time :
1:0

I have also tried to delete the carriage return on the first line "Home Time :", but this did not work either.

Can someone please point me into the right direction as to how can I get the output on a single line.

Thanks in advance
Lawrence
 
Hi

As you explicitly mentioned Bash, then there is a dedicated [tt]-n[/tt] switch :
Code:
echo [highlight]-n[/highlight] [i][green]"HOME Time :"[/green][/i]

See [tt]help echo[/tt] command for details :
help echo said:
[pre]echo: echo [-neE] [arg ...]
[silver](...)[/silver]
Options:
-n do not append a newline[/pre]

In other shells the solution is to add a training [tt]\c[/tt] to the string. That works in latest Bash versions, too, though you have to make sure escape sequences are interpreted :
Code:
echo [highlight]-e[/highlight] [i][green]"HOME Time :[/green][/i][COLOR=lime yellow]\c[/color][i][green]"[/green][/i]

As a portable solution used to be suggested to use [tt]printf[/tt] instead :
Code:
[b]printf[/b] [i][green]'%s'[/green] [green]"HOME Time :"[/green][/i]

[gray]# if you are sure the string to print does not contain %[/gray]
[b]printf[/b] [i][green]"HOME Time :"[/green][/i]

Note that [tt]printf[/tt] could also help formatting that time nicer by padding it with 0's :
Code:
[b]printf[/b] [i][green]'HOME Time : %02d:%02d[/green][/i][lime]\n[/lime][i][green]'[/green][/i] [navy]$(([/navy] [teal]([/teal]ENDHOME - STARTHOME[teal]) /[/teal] [purple]60[/purple] [navy]))[/navy] [navy]$(([/navy] [teal]([/teal]ENDHOME - STARTHOME[teal]) %[/teal] [purple]60[/purple] [navy]))[/navy]


Feherke.
feherke.github.io
 
Thanks @feherke, that worked like dream

Lawrence
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top