1

I am running this bash script:

#!/bin/sh
date +"%Y-%m-%d %H:%M:%S " | tr -d 'n' >> /home/test_a5gh/Pub_IP.txt
curl http://checkip.amazonaws.com?newline -s >> /home/test_a5gh/Pub_IP.txt
cat /home/test_a5gh/Pub_IP.txt

After creating the above file, I placed it in the folder /home/test_a5gh/ and then I made the script executable:

$ sudo chmod u+x testip.sh

Next, to test the script, I ran:

$ ./testip.sh

However, the output of this command is being produced on 2 lines. Here is an example:

2011-22-33 16:10:27 
111.222.333.444

I need the output to appear on 1 line, like this:

2011-22-33 16:10:27 111.222.333.444

Additional Information:

The script is taken from here.

Question:

Is there a way to get this output on 1 line?

1 Answer 1

2

Here's one way:

#!/bin/bash

mydate=$(date '+%Y-%m-%d %H:%M:%S')
myip=$(curl http://checkip.amazonaws.com?newline -s)

printf '%s %s\n' "$mydate" "$myip" >> /home/test_a5gh/Pub_IP.txt

FYI, you don't need sudo to chmod files you own in your own home directory.

1
  • Hey, that is a great answer! It worked. Could you explain what changes you made?
    – edesz
    Jun 25, 2016 at 1:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .