I've probably tooted about this before, but I don't know why this isn't standard.

It's just so obvious, at least to me. ;)

~ $ type mcd mcd is a function mcd () { [[ -n $1 ]] && mkdir "$1" && cd "$1" }

#shell #UnixShell #ShellScripting #sh #bash #ksh #mksh

@rl_dane I don't get it

@joel @rl_dane

I think it makes a new directory and then changes the current working directory to be the new directory

@pete @joel @rl_dane first it checks if it exists, if I remember my bash.

@joel

(((Ok, I'm going to ELI5 this for you (gladly), but you really gotta read it all carefully, ok?)))

Very simple:

function mcd() { [[ -n $1 ]] && mkdir "$1" && cd "$1" }

First of all, you've got the function declaration itself:

function mcd() { ... }

Then the guts:

[[ -n $1 ]] && mkdir "$1" && cd "$1"

The && are a logical and, basically "do the next thing if the previous thing completed successfully. So you could read it as:

if [[ -n $1 ]]; then if mkdir "$1"; then cd "$1" fi fi

The [[ -n $1 ]] basically means "return true if variable $1 (first argument) isn't blank."

And you already know what mkdir and cd do. ;)

@sotolf @pete @hyde

@joel @rl_dane i have the same i think ... Need to check mine

@joel @rl_dane

mine is actually a bit different

mkd() { [[ -n $1 ]] && mkdir -p "$1" && cd "$_" }

@hyde @joel

It's totally valid, but I'm curious as to your reasoning for using $_ instead of $1. I only ever use that variable interactively. ;)

Actually, no. I use !$, which is a bash-ism, and not actually a variable. ;)

@rl_dane Actually, I dont know ... I use it often in terminals and I must have been a habit I have πŸ€·β€β™‚οΈ
@joel
@hyde @rl_dane @joel $_ is a perl thing at least where I think it just resolve to the last set variable, or something like that, you often use it in for loops and stuff.

@sotolf @hyde @joel

I think Perl adopted it from POSIX shell.

@sotolf @hyde @rl_dane @joel PowerShell also decided to use $_ for current item.

@dmoonfire @sotolf @hyde @joel

Powershell uses the dollar sign for variables? That's a marked improvement over %CURSED% or whatever the predecessor used ;)

DOS Batch was so weirdly limited. It was frankly painful.

@rl_dane @dmoonfire @hyde @joel Yeah, it uses $ for all variables, it doesn't have the scalar, collection thing that perl has, I guess it's for easy interpolation or something like that.

@sotolf @dmoonfire @hyde @joel

I might be the only one, but I'm a little sad that #RokuLang dropped the thing where "@array" becomes "$array" when referencing a scalar element from Perl.

But don't anyone take that as a serious criticism. I haven't tried roku yet and haven't so much as touched a Perl script in a couple years.

Used it a fair bit in the naughties, though. Still have a fondness for it, but I'm out of practice.

@rl_dane @dmoonfire @hyde @joel

PowerShell is surprisingly good at doing windows stuff, it still uses @ to create hashmaps if I remember correctly And a lot of $ everywhere :p

$curIln = $file.FullName.Split('_')[1] $kunde = '' if ($ilns.Contains($curIln)) { $kunde = $ilns[$curIln] } else { $kunde = $curIln } # Gruppe erstellen wenn nicht vorhanden, und dann zu kundengruppe hinzufΓΌgen if (!$knd_grp.Contains($kunde)) { $knd_grp[$kunde] = [System.Collections.ArrayList]@() } $knd_grp[$kunde].Add($file)

@sotolf @rl_dane @hyde @joel PowerShell does a lot of things right. I like the ability to pass objects instead of purely text, that is awesome.

And you can use / for path separators instead of always needing \.

I do not like that backticks are the escape character instead of \.

I do not enjoy using the language for bigger projects though. We've had a reasonable large system that I've trimmed down over the years to only a few 10ks worth of LOC.

@dmoonfire @rl_dane @hyde @joel

10ks of lines are huge powershell scripts yeah, mine are mostly just for smaller tasks, like parsing some automated mails or files and then send mails and stuff like that. I don't think I've written anything more than 3-400 lines in a script, I really believe when you come up in the ks it could get unweildy quite quick yeah :)

@sotolf @rl_dane @hyde @joel It's gotten a lot smaller than it used to be.

$ tokei =============================================================================== Language Files Lines Code Comments Blanks =============================================================================== C# 7723 1556305 1123506 184816 247983 PowerShell 151 12998 9314 1890 1794 SQL 16247 9019795 7701471 703960 614364 ... removed a lot of lines =============================================================================== Total 31510 26172744 24008789 1256384 907571 ===============================================================================
@rl_dane @dmoonfire @hyde @joel And yeah, Batch is so hard to get right, and to understand, and to look at, I think the longest bat script I've written that did something useful was 10 lines, and it was painful.

@sotolf @dmoonfire @hyde @joel

Same. It's like if really old POSIX shell and BASIC had a really unhealthy child.

@rl_dane @dmoonfire @hyde @joel ah found it :p behold and admire :p

set WINSCP_RESULT=%ERRORLEVEL% if %WINSCP_RESULT% equ 0 ( echo Success ) else ( echo Error ) pause for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%" set "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"

@sotolf @dmoonfire @hyde @joel

That's actually more readable and understandable than I remember. XD

Having to call a commandline option to for is pretty cursed. XD

@rl_dane @dmoonfire @hyde @joel

I also especially wrote it to be as readable as I could as I know if it would ever fail I wound have to try to understand it.

Yeah, I really enjoy the command line option for for as well ;)

@sotolf

It's the last argument of the previous command executed.

In this case there isnt a real advantage, but sometimes when you don't want to type a long word, it can help

@rl_dane @joel

@rl_dane Absolutely. Used it for years. On my machines it’s called `mkcd`.

@rl_dane I also have something like this called "mkdircd" 

More gems I use a lot:
- wping()='curl -Lv "$1"' > /dev/null'
- dctl='systemctl'
- dctlu='systemctl --user' (yes I run some of my own background services + regular miantenance on systemd, bring in the pitchforks )

@Parnikkapore

Ooo, I like wping!

@rl_dane @Parnikkapore

about curl, have you seen
https://hurl.dev ?

Hurl - Run and Test HTTP Requests

Hurl, run and test HTTP requests with plain text and curl. Hurl can run fast automated integration tests.

@hyde @Parnikkapore

Looks interesting, although I must admit I'm kind of a dinosaur when it comes to web stuff, and didn't quite understand it.

That's why you'll see me doing silly stuff like web scraping using sed. XD

@rl_dane @hyde *hides in current pet project doing web scraping with XPath in Racket, despite the XML fad happening before I was born*

@Parnikkapore

Now that I think of it, using netcat would probably be a more efficient implementation of wping, such as

function portcheck { nc -zw1 "$1" "$2" &>/dev/null } function wping { local port successports=() host="${1:-}" for port in 80 443; do portcheck "$host" $port && successports+=($port) done case ${#successports[@]} in 0) echo "$host is down"; return 1;; 1) echo "$host is up (port ${successports[@]})";; *) echo "$host is up (ports ${successports[@]})";; esac }

@rl_dane wping is not a host-up checker, it's a website-up checker :D

(and, admittedly, often a "why is my internet not working" debugging tool)

@rl_dane Alternate-universe me might've taken in a variant of your function as `tcping` though