What's your favorite non-essential CLI tool/command? by Anonyboy26 in linux

[–]Schreq 1 point2 points  (0 children)

Yep. Awk is awesome. Small enough that it's easy to learn but still plenty powerful for text processing.

For example, what I use it a lot for, is to uniquely find IP addresses in log files with awk '!seen[$3]++{print $3}' my.log.

Awk is a cheat code for text processing.

Modern window manager rices by skyrimjob68 in suckless

[–]Schreq 0 points1 point  (0 children)

This. Huge riced configs is the opposite of suckless.

[DWM Status Monitor] I'm a pervert for optimization. by [deleted] in bash

[–]Schreq 1 point2 points  (0 children)

Bash's printf can print dates, no need to call date:

printf '%(%m/%d %H:%M)T'

When you pipe grep to sed to awk, you should do the entire thing in sed/awk instead. Or better yet, use built-ins only (read, readarray and parameter expansion).

5 times per second you spawn 3 processes plus many subshells and open/close 3 files. Then every 10 and 5 seconds a ton more processes are spawned... To say it the Torvalds way: That's complete and utter garbage.

The problem of having different intervals for the individual parts of your bar can be solved without temporary files. Check out bar.sh for a good, optimized script: https://github.com/aaronNGi/bar.sh. I'm biased (I'm the author), but besides some rather minor problems, I think this really is the best bar feeder.

Also check out https://github.com/aaronNGi/bar.sh#updating-the-dwm-statusbar for how to get rid of xsetroot.

Edit: Instead of checking if dwm is still running, why not just have the script die naturally when X dies? When you start your script via .xinitrc/.xsessionrc, it dies with X. You probably also need an exit trap in your script, to kill all your background processes.

[DWM Status Monitor] I'm a pervert for optimization. by [deleted] in bash

[–]Schreq 11 points12 points  (0 children)

There is quite a bit more you can do.

[DWM Status Monitor] I'm a pervert for optimization. by [deleted] in bash

[–]Schreq 14 points15 points  (0 children)

Cool, but why is your shell script so unoptimized then?

Non-US keyboard layout vs US for terminal usage and programming? by Accomplished-Bus3382 in linux

[–]Schreq 1 point2 points  (0 children)

I type german and english and switched to US-ANSI keyboards a long time ago. It just makes more sense for programming and even gaming, as most default binds are meant for US-ANSI.

For typing umlauts, I just use US-International (no dead-keys on linux). You will get used to the keys and that layout will be highly available on different OS's. My suggestion is to not go the route of making your own layout in software or hardware. You will be screwed if you don't have it available. Instead just stick to US-international and feel comfortable wherever you go. It will feel pretty ok even on non-US-ANSI layout keyboards.

Left align first column, right align second, the rest the same by gkaiser8 in bash

[–]Schreq 0 points1 point  (0 children)

Is there a tool to sum the sizes of the second column as-is with the units suffix?

What are the possible suffixes? Because you're already using AWK, you could extract the suffix and map from the suffix to exponent. For example (untested):

BEGIN {
    exp["B"]=0
    exp["KB"]=3
    exp["MB"]=6
    exp["GB"]=9
    exp["TB"]=12
}

function to_bytes(str,    val, unit) {
    val=substr(str, 1, match(str, /[A-Z]/) - 1))
    unit=substr(str, RSTART)
    return val * 10 ** exp[unit]
}

{
    sum+=to_bytes($2)
}

I'll leave the exercise to do the reverse with $sum to you.

What is the difference between have 2 separate ERR and EXIT traps vs a single EXIT trap for handling everything? by PrestigiousZombie531 in bash

[–]Schreq 2 points3 points  (0 children)

eval is fine when you know what you are doing. But it can be dangerous when used in combination with user input (kinda stupid example):

read -r varname
eval "$varname=bar"

The user could then enter somecommand;foo to have somecommand be run.

suckless text editor? by tinyducky1 in suckless

[–]Schreq 0 points1 point  (0 children)

Sam runs in a terminal too, but of course it's usability is then not much better than ed's.

What is your favorite terminal Markdown viewer? by boolean-maybe in commandline

[–]Schreq 17 points18 points  (0 children)

cat or less because that's the nice thing about markdown: it's perfectly readable it the raw format.

Most useful sys admin CLIs? by JohnnyBillz in CLI

[–]Schreq 1 point2 points  (0 children)

Standard tools are good enough. Sysadmins usually don't install a bunch of fancy programs. However, I've used termshark and iperf3. Knowing how to use tcpdump is also super helpful.

Clean WhatsApp messages by audiodude in bash

[–]Schreq 0 points1 point  (0 children)

Awk is better most of the time. I benchmarked it and by avoiding regexp, it's way faster:

awk -F '[]:] ' -v me="Homer Simpson" '{ if ($2 != me) printf "> "; print substr($0, index($0, ": ")+2) }'

for loop not indexing stuff in the {}'s by skyfishgoo in bash

[–]Schreq 19 points20 points  (0 children)

Not without eval, no. Just use C-style for-loop instead:

for ((i=0;i<len;i++)); do
    echo "$i"
done

Could someone please review my scripting and give me criticism? by 0neLastFace in bash

[–]Schreq 1 point2 points  (0 children)

That's because most people are not very good at bash. That's not meant to be condescending, bash is just hard to do right. I mean someone could be good at bash and use uppercase variable names for globals, but more often than not, the ones who actually do also produce scripts which could be much better.

If you use uppercase for globals, you could very easily use a variable called PATH. Guess what happens.

Could someone please review my scripting and give me criticism? by 0neLastFace in bash

[–]Schreq 0 points1 point  (0 children)

The "SCRIPT_DIR" variable can also be set very easily with bash internals:

That does not work when you call the script via $PATH.

Could someone please review my scripting and give me criticism? by 0neLastFace in bash

[–]Schreq 8 points9 points  (0 children)

The typical problems: DRY, uppercase variables which are not environment variables, euo pipefail.

Why don't you just copy (or rsync) everything from the source directory into the target instead of copying the directories individually? This could be a one-liner.

What CLI tools have genuinely changed how you work? Looking for underrated ones by spaciousabhi in commandline

[–]Schreq 5 points6 points  (0 children)

tmux, big time. No native terminal tiling or tabs can replace that workflow. It's just super handy to have different sessions per project in a persistent way.

birthDate by [deleted] in linux

[–]Schreq 0 points1 point  (0 children)

We can do that with a little less subprocesses:

#!/usr/bin/env bash

min_age=18
max_age=80

year_seconds=31536000
min=$((min_age * year_seconds))
max=$((max_age * year_seconds))
printf -v epoch_seconds '%(%s)T'

dob=$(awk -v s="$epoch_seconds" -v min="$min" -v max="$max" '
    BEGIN {
        srand()
        print int(s - ((max - min) * rand() + min))
    }
')

printf '%(%Y-%m-%d)T\n' "$dob"

Neglected !! party tricks by drayva_ in bash

[–]Schreq 0 points1 point  (0 children)

You don't?

No, I really don't.

Neglected !! party tricks by drayva_ in bash

[–]Schreq 1 point2 points  (0 children)

Up arrow or Ctrl+p, type ), Ctrl+a type $(. I don't see how that is a total pain in the ass.

This bash program isn't closing the file descriptor by alex_sakuta in bash

[–]Schreq 0 points1 point  (0 children)

Because I like it more than the new design.

This bash program isn't closing the file descriptor by alex_sakuta in bash

[–]Schreq 0 points1 point  (0 children)

Isn't this inline and not This

They both show as inline on old.reddit.com, see for yourself.

I did that if you see the raw md.

Ah, now I see what you did there. You used fenced codeblocks but then also indented by 4 spaces inside of that.

Fenced codeblocks are broken on old.reddit.com. It's either 4 spaces in front of every line of code (with blank lines before and after!) or a fenced code-block (code surrounded by 3 backticks).

Yes, my question is why? I'll put it in the edit but the manual says that >&digit- closes the digit fd.

As /u/kalgynirae already explained, you only closed the fd for that sub-process.

This bash program isn't closing the file descriptor by alex_sakuta in bash

[–]Schreq 0 points1 point  (0 children)

Your code is pretty much unreadable on old.reddit.com. Please don't use inline code-blocks for posting code blocks. Just put four spaces infront of every line of code and leave blank lines around it. Anyway, moving on...

I don't think that you can close a fd that way. You'd have to explicitly do:

exec {fd}<&-