I am tired of creating a file with nano, saving it and then making it executable. Is there a command that makes it in one step?

  • @[email protected]
    link
    fedilink
    35
    edit-2
    7 months ago

    Write an alias/function to do it and add to your bashrc.

    function nanox() {
        nano "$1"
        chmod +x "$1"
    }
    
    • @psmgx
      link
      06 months ago

      This is the way.

  • @NegativeLookBehind
    link
    English
    16
    edit-2
    7 months ago

    You want the install command.

    install

    At least, I think this will do it. I haven’t used it in a while.

    • 2xsaiko
      link
      fedilink
      26 months ago

      install -m755 /dev/null target was the first thing I thought of. I would never use this but it is a single command.

        • 2xsaiko
          link
          fedilink
          16 months ago

          I’m going to write (at least part of) the script first anyway, and then I can just use chmod +x after the file is saved which is shorter.

  • @kayaven
    link
    127 months ago

    You could define a function that takes a parameter, which touches a file with the parameters value, chmods it and then opens it with nano?

    create_exec() {
        touch "$1"
        chmod +x "$1"
        nano "$1"
    }
    

    Then you could type create_exec file.sh and it would do the rest for you.

  • @[email protected]
    link
    fedilink
    117 months ago

    Here’s one I have saved in my shell aliases.

    nscript() {
        local name="${1:-nscript-$(printf '%s' $(echo "$RANDOM" | md5sum) | cut -c 1-10)}"
        echo -e "#!/usr/bin/env bash\n#set -Eeuxo pipefail\nset -e" > ./"$name".sh && chmod +x ./"$name".sh && hx ./"$name".sh
    }
    alias nsh='nscript'
    

    Admittedly much more complicated than necessary, but it’s pretty full featured. first line constructs a filename for the new script from a generated 10 character random hash and prepends “nscript” and a user provided name.

    The second line writes out the shebang and a few oft used bash flags, makes the file executable and opens in in my editor (Helix in my case).

    The third line is just a shortened alias for the function.

  • chi-chan~
    link
    76 months ago

    Use && to use multiple commands one after the other, don’t use ;.

    • chi-chan~
      link
      186 months ago

      - && means execute if the command before ended successfully

      - || means execute if the commnad before failed

      - ; just means execute the command - no matter if succeeded or failed

      • @[email protected]
        link
        fedilink
        16 months ago

        My dude, thanks for this. I’ve been using && for a long time now but never knew the rest, I’m still pretty new to linux comparatively.

  • Björn Tantau
    link
    fedilink
    37 months ago

    You could append the chmod command with && but that’s probably not what you wanted.