Ubuntu: Need bash script output on one line - curl and >> produces 2 lines



Question:

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?


Solution:1

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »