5

I've been setting up a script to work with haste-server. This takes piped or file input (tail /var/log/messages | haste and haste < /path/to/file.txt) and submits it to the server which then outputs a in my terminal. See below:

#!/bin/bash
url="http://hastebin.com"
key="$(curl --silent --data-binary @/dev/fd/0 $url/documents | cut -d "\"" -f 4)"
echo "$url/$key"

It works just fine, however it adds a trailing new line to the input. How can I read @/dev/fd/0 to remove the \n new line?

Edit: Here is my completed script for submitting a haste that trims the newline:

#!/usr/bin/env bash

url="http://hastebin.com"
data=$(< /dev/fd/0)
key="$(printf "%s" "$data" | curl -X POST -s --data-binary @- "$url/documents" | cut -d "\\"" -f 4)"
echo "$url/$key"
  • The output is a? Where is the newline in that? You re adding a newline with the echo command use echo -n instead. – Anthon Feb 17 '15 at 9:52
  • @Anthon I'm looking to trim the input sent using curl, not the echo itself. – Brian Graham Feb 17 '15 at 22:05
  • If the target is a terminal for display's sake, then you don't necessarily need to strip the newline - just add an escape at the end (or before the end) to either scroll the terminal or to eat the newline. You can [ -t 1 ] test for a terminal on stdout, and tput can help with the escapes. If you're having difficulty with the eat the newline thing, printf '\033[@\n' and have a look at how it behaves, – mikeserv Feb 18 '15 at 11:05
3

avoiding newlines by 'echo' command

Instead of echo, use echo -n.

If that does not work (e.g. on OSX with /bin/sh as shell), or if you want make your script independent of which shell it runs under, use /bin/echo -n.

avoiding newlines from "payload" (here: '$key')

change the output newlines with tr, e.g. echo "$url/$key" | tr '\n' '|'

  • And you, instead of space use "." at the end of your sentences. :-) – peterh - Reinstate Monica Feb 17 '15 at 10:05
  • 3
    If you really want to make it portable, use printf; see unix.stackexchange.com/questions/65803/… for details. – Stephen Kitt Feb 17 '15 at 13:17
  • I said it added a trailing new line to the input, meaning inside the curl when submitted. I have edited the code so that it works, submit a small file and you'll see what i mean when you visit the hastebin. – Brian Graham Feb 17 '15 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.