28
submitted 1 month ago by [email protected] to c/[email protected]

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?

all 19 comments
sorted by: hot top controversial new old
[-] [email protected] 72 points 1 month ago

You mean like

touch file && chmod +x file ?

[-] [email protected] 18 points 1 month ago

Wrap it up folks, we're done here.

[-] [email protected] 39 points 1 month ago* (last edited 1 month ago)

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

function nanox() {
    nano "$1"
    chmod +x "$1"
}
[-] [email protected] 0 points 1 month ago

This is the way.

[-] [email protected] 17 points 1 month ago* (last edited 1 month ago)

You want the install command.

install

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

[-] [email protected] 6 points 1 month ago

touch file && chmod +x file is good but this here is the one true command for the purpose.

[-] [email protected] 2 points 1 month ago

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

[-] [email protected] 1 points 1 month ago

Why would you never use it?

[-] [email protected] 1 points 1 month 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.

[-] [email protected] 13 points 1 month 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] 12 points 1 month 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.

[-] [email protected] 7 points 1 month ago

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

[-] [email protected] 18 points 1 month 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] 1 points 1 month 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.

[-] [email protected] 6 points 1 month ago

touch file && chmod +x file

[-] [email protected] 3 points 1 month ago

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

this post was submitted on 03 May 2024
28 points (86.8% liked)

Linux

45443 readers
1312 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS