0

I was writing a simple bash script for a small task.

 echo starting
 URL="10.10.0.1/api/monitoring/traffic-statistics"
 curl -b cookie $URL

When I executed it using bash script.sh , it just prints curl -b cookie pi/monitoring/traffic-statistics. It's as if the file said echo curl -b cookie $URL and somehow URL got corrupt. When I type cat script.sh, instead of listing the file contents, it just displays a part of one line in the script. Note: I was editing the file in vim, and it was displaying it without any issue. Nano editor also showed the same content. But when it comes to bash, it messes up the order, either when executing the file or just dumping the contents. This is the hex dump. (xxd script.sh)

00000000: 6563 686f 2073 7461 7274 696e 670d 5552  echo starting.UR
00000010: 4c3d 2231 302e 3130 2e30 2e31 2f61 7069  L="10.10.0.1/api
00000020: 2f6d 6f6e 6974 6f72 696e 672f 7472 6166  /monitoring/traf
00000030: 6669 632d 7374 6174 6973 7469 6373 220d  fic-statistics".
00000040: 6375 726c 202d 6220 636f 6f6b 6965 2024  curl -b cookie $
00000050: 5552 4c0d                                URL.

I can't seem to find any stray \r character either. What did I do wrong here and how do I produce a proper file that bash will accept?

  • 1
    You forgot the shebang and you use CR (x0d) instead of LF (x0a) in your sh file. – klanomath Jul 10 '17 at 9:15
  • Yep that did the trick. I had somehow misconfigured my vimrc and then read 0x0d as LF and thought it's find. I didn't think much about the shebang because i was running it with bash command. – chamod Jul 10 '17 at 9:24
  • Will you put it as an answer so I can accept it. – chamod Jul 10 '17 at 9:24
  • 1
    The shebang isn't really necessary here – nohillside Jul 10 '17 at 9:49
1

You misconfigured your vimrc and a carriage return (CR or 0x0d) instead of a line feed (LF or 0x0a) is entered/read.

So modify the vimrc back to the standard behavior.

The missing shebang (e.g. #!/bin/bash) at the beginning is not the cause of the failing script. I would include it though - just for redundancy.

You must log in to answer this question.

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