News for hackers. Stuff that matters.
Re:Use long flags when scripting (2013) by m463 on Friday September 18, 2020 @01:08PM (#24519260)
In scripts, I usually do stuff like this

  command \
    --longopt1 \
    --longopt2 arg \
    argument
or

  command | \
    command 2 | \
    command 3 \
      --opt1 \
      --opt2 \
      arg
Re:Use long flags when scripting (2013) by inshadows on Friday September 18, 2020 @01:51PM (#24519787)
It's nice but you cannot comment individual lines. For that you can go one step further and use arrays:

  cmd=(
    command
    -x -y
    --bar OPTARG
    ARG ARG
  )
  $cmd
This works in Zsh. In Bash that would be ${cmd[@]} I think. I use this with long qemu command lines where I often modify and comment out arguments during testing.
Re:Use long flags when scripting (2013) by tdons on Friday September 18, 2020 @02:58PM (#24520611)
You can comment using a hack; check this out:

  command `# a comment` \
    --longopt1 `# another comment` \
    --longopt2 arg \
    argument
Re:Use long flags when scripting (2013) by inshadows on Friday September 18, 2020 @03:47PM (#24521147)
I didn't mean commenting arguments for documentation but commenting them out while you're writing and testing the script. I.e. this won't work:

  command `# a comment` \
    # --longopt1 \
    --longopt2 arg \
    argument
AFAIK there's no hack for this.

EDIT: Oh, `# a comment`. Didn't notice that. I'm going to explore it, thanks.

Re:Use long flags when scripting (2013) by lhoursquentin on Monday September 21, 2020 @06:12AM (#24541641)
You should probably note that this hack spawns an additional shell process per comment :)
Re:Use long flags when scripting (2013) by periodontal on Friday September 18, 2020 @02:12PM (#24520035)
For bash, add quotes: "${cmd[@]}"

Otherwise, you'll get word splitting. E.g.,

  cmd=(
   test
   "!= !="
   !=
   ""
  )
  "${cmd[@]}" && echo Success
  ${cmd[@]} || echo Failure
Re:Use long flags when scripting (2013) by viseztrance on Friday September 18, 2020 @01:23PM (#24519458)
If you have a very long command you can run ctrl x e (hold control press x, than e) and edit it in your $EDITOR.
Re:Use long flags when scripting (2013) by InvaderFizz on Friday September 18, 2020 @03:20PM (#24520844)
I wish I had known this 20 years ago. Thanks!
Re:Use long flags when scripting (2013) by BrandonM on Friday September 18, 2020 @01:51PM (#24519791)
You don't even need the \ if the | is the last character of the line.
Re:Use long flags when scripting (2013) by 52-6F-62 on Friday September 18, 2020 @01:13PM (#24519319)
Wow. I just realized I'm terrible for this. I usually format my programming pretty verbosely to make it future-readable but I'm terrible for not doing that in my scripts.

I'll add a comment or something explaining it but now I feel guilty and should probably go back and rewrite a few lines...

I mean—for long scripts I'll break them up but I haven't paid much mind to arguments/flags.

Re:Use long flags when scripting (2013) by dmurray on Friday September 18, 2020 @01:25PM (#24519483)
I do this if I have to look up the right flag to use. If it's something commonly seen like "grep -o" or "tar -xvzf" I assume whoever is maintaining the script (probably future me) will be more familiar with the abbreviations than the long versions.
Re:Use long flags when scripting (2013) by 52-6F-62 on Friday September 18, 2020 @01:45PM (#24519706)
Yeah good point. It did strike me, though, because I'd just written several scripts running JMeter commands and sort of breezed over how they're written even though I had to go and look up all the flags.
Re:Use long flags when scripting (2013) by coldacid on Friday September 18, 2020 @03:53PM (#24521208)
If I'm splitting across lines, I prefer to put the pipe _after_ the newline. Otherwise, I like the cut of your jib, and will likely do this with arguments in scripts from now on.
Re:Use long flags when scripting (2013) by m463 on Saturday September 19, 2020 @02:17PM (#24529399)
sometimes I do something like this:

  foo \
    --group1 \
    \
    --group 2 \
    --more-stuff 2 \
    | \
   bar \
    --bar-stuff
using a line with a single backlash or breaking out the pipe can make grouping stuff clear. well, I mean as clear as having to use backslashes which to me is a little inelegant. Personally python and its indenting strategy has grown on me especially since editor support makes it painless to use and visually excellent.
Re:Use long flags when scripting (2013) by kl4m on Friday September 18, 2020 @02:19PM (#24520117)
Downside: forgetting a \ will truncate the flags list, leading to unexpected behaviour followed by an error.
Re:Use long flags when scripting (2013) by GordonS on Friday September 18, 2020 @03:17PM (#24520813)
This is how I always do it do - I find it much more readable, and it leads to better diffs too.