×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Bash Onliner ...

Bash Onliner ...

Bash Onliner ...

(OP)
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

RE: Bash Onliner ...

Hi

As you explicitly mentioned Bash, then there is a dedicated -n switch :

CODE --> Bash ( fragment )

echo -n "HOME Time :" 

See help echo command for details :

Quote (help echo)

echo: echo [-neE] [arg ...]
(...)
    Options:
      -n        do not append a newline 

In other shells the solution is to add a training \c to the string. That works in latest Bash versions, too, though you have to make sure escape sequences are interpreted :

CODE --> Bash ( fragment )

echo -e "HOME Time :\c" 

As a portable solution used to be suggested to use printf instead :

CODE --> shell ( fragment )

printf '%s' "HOME Time :"

# if you are sure the string to print does not contain %
printf "HOME Time :" 

Note that printf could also help formatting that time nicer by padding it with 0's :

CODE --> shell ( fragment )

printf 'HOME Time : %02d:%02d\n' $(( (ENDHOME - STARTHOME) / 60 )) $(( (ENDHOME - STARTHOME) % 60 )) 

Feherke.
feherke.github.io

RE: Bash Onliner ...

(OP)
Thanks @feherke, that worked like dream

Lawrence

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close