4 ms·
Nushell is great for simple tasks. i.e. ls **/data.json | each {|it| mv $it.name $"(dirname $it.name)/data.yml" } Will be much more complex in bash find
by maxloh 4y ago
Nushell is great for simple tasks. i.e.
ls **/data.json | each {|it| mv $it.name $"(dirname $it.name)/data.yml" }
Will be much more complex in bash
find -name "data.json" | xargs -i bash -c "mv \"{}\" \"\$(dirname '{}')/data.yml\""
Note the escaped quotes and dollar signs in bash. It is really difficult to get them right.
In Nutshell we are actually using "functions" (supported syntax-wise) to do the job, so it will be much more simpler.
---
A more complex example from my recent code:
ls $"(which npm.ps1 | get 0.path | path dirname)/*.ps1" | each {|it| get name | path basename }
| each {|script_file| $"alias ($script_file | str replace ".ps1" "") = powershell.exe ($script_file)" }
| str join "\n" | save --force ~/.config/nushell/env-generated.nu
Will be something like this in bash,
find "$(dirname "$(which npm.ps1)")/" -maxdepth 1 -name "*.ps1" | sed -r "s/^.*\/([^\/]+)\.ps1/alias \1 = powershell.exe \1.ps1/g" >> "$NU_CONFIG_FILE"
Although the bash one is shorter, it takes me 10x more time to write and test. I initially tried to write it with xrgs but given up due to the complexity involved.
I think Nushell is more readable and easy to write for me.
- darrenf 4y ago> find -name "data.json" | xargs -i bash -c "mv \"{}\" \"\$(dirname '{}')/data.yml\"" Albeit this task needn't involve a shell at all - just `find` and `rename`: find . -name data.json -exec rename .json .yml {} +
- akkartik 4y agoDo you really have rename on your computer? What OS is this?
- nosklo 4y agoI'm not op but I do have `rename` in my computer. I'm using latest fedora linux. It seems to be included with the util-linux package https://packages.fedoraproject.org/pkgs/util-linux/util-linux/fedora-rawhide.html#files https://packages.fedoraproject.org/pkgs/util-linux/util-linu...
- arp242 4y agoIn zsh you can use: for f in **/data.json; mv $f $f:r.yaml Which is even shorter, and easier. In bash you need a bit more syntax: for f in **/data.json; do mv $f ${f%.json}.yaml; done That second example can probably also be simplified a lot in a similar fashion.
- Arch-TK 4y agoI'm sorry but obviously you had to learn how to use nushell but you didn't learn how to use find (you don't need xargs) or bash (you don't need find). Your first problem solved in bash: shopt -s globstar for f in ./**/data.json; do mv "$f" "${f%json}yml"; done You can do it without globstar (without running into weird issues with newlines) like so: find . -name data.json -print0 | while read -rd '' f; do mv "$f" "${f%json}yml"; done Now, since the particular problem doesn't require a shell at all, you can solve it entirely in find: find . -name data.json -execdir mv {} data.yml \; Your second example would be: ( cd "$(which npm.ps1)" && \ for f in *.ps1; do printf "alias %s='powershell.exe %s'\n" "${f%.ps1}" "$f"; done ) >file
- wodenokoto 4y ago> Now, since the particular problem doesn't require a shell at all, you can solve it entirely in find: find . -name data.json -execdir mv {} data.yml \; How do you do that without a shell?
- maxloh 4y agoIIRC running a command without piping/string semantics doesn't involve a shell, such as os.system() in python and child_process.exec() in Node.js.
- Arch-TK 4y agoI mean obviously you need some way of executing the command and you're probably not going to write a C program to call execve to do it, but the point is that it doesn't involve any shell features and aside from the escape syntax for the semicolon is completely shell independent.
- maxloh 4y agoI didn't learn Bash from scratch. I just serach Google/StackOverflow for similar questions. But that is the point, there are too many commands to do the same job in Bash, each with dozens of flags. It is hard to learn them all, or even find the right command (like shopt, read) or flag (like -rd, print0, -execdir) in the first place. That's why Nutshell will be simpler for small tasks.