You are not logged in.

#3476 2021-01-17 19:38:07

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

The script is, it's just assuming echo has an -e flag which is a safe assumption on an arch system - in fact I don't know if there is an implementation of echo available in the repos that doesn't support the -e flag.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3477 2021-01-17 19:42:05

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,111
Website

Re: Post your handy self made command line utilities

I don't know whether it counts, but the tcsh built-in does not support it:

$ tcsh
> which echo
echo: shell built-in command.
> echo -en Hello
-en Hello
> 

Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#3478 2021-01-17 19:42:19

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

@Trilby, True, but far as I know flags for echo are not defined in POSIX.
So how would you change that than?

Last edited by qinohe (2021-01-17 19:43:27)

Offline

#3479 2021-01-17 19:52:40

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

I didn't suggest changing anything.  I just rejected your claim that the script isn't POSIX.  "systemd-analyze" and "journalctl" are also not defined in POSIX, that doesn't mean the script isn't POSIX compliant.  The script doesn't use any shell features that are not provided by POSIX unless your /bin/sh includes a built in echo that doesn't have -e.  I wasn't aware of tcsh's echo, but I don't think tcsh could/should provide a /bin/sh as tcsh is most definitely not POSIX.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3480 2021-01-17 20:00:33

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

Okay I follow your rationale, but than I'm always trying to follow the rules for POSIX to precise.
I mean it would be safe to say the script should contain a line 'Arch only' for I think there enough distro's/OS's following POSIX strict?

Offline

#3481 2021-01-17 20:03:54

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

Is that a question?  There are far more distros that would fail on due to not running systemd than to not having echo's -e flag.  Do you know of any?


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3482 2021-01-17 20:30:36

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,272
Website

Re: Post your handy self made command line utilities

Trilby wrote:

Do you know of any?

It's not a distribution but OpenBSD's echo(1) doesn't support it:

~$ /bin/echo -e '/thello'
-e /thello
~$

And yes, the systemd commands would also fail for that operating system :-)


Para todos todo, para nosotros nada

Offline

#3483 2021-01-17 20:38:02

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

Trilby wrote:

There are far more distros that would fail on due to not running systemd than to not having echo's -e flag.  Do you know of any?

That's true, the OS part in my previous part had to do with 'echo' only, BSD don't run systemd ... sure, but it handles 'echo' strictly POSIX.
I don't know of any distro running 'echo' without the -e flag, no. So, it's save to assume the script is POSIX for most shells that is.

Still, I would suggest to use 'printf' in OP's script (for it's formatting) but that's beside the POSIX discussion.

edit: HoaS said it too. was getting coffee, what to do without coffee...;)

edit: @Ferdinand,
I missed the 'last login' command, 5 pipes...;)
Maybe you can do it in one but:
last | grep -v reboot | awk '{print $1"   " $3"   "$4"   " $5"   " $6"   " $7} NR == 5{exit}'

Last edited by qinohe (2021-01-17 21:42:17)

Offline

#3484 2021-01-17 21:52:02

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Post your handy self made command line utilities

Trilby wrote:

I didn't suggest changing anything.  I just rejected your claim that the script isn't POSIX.  "systemd-analyze" and "journalctl" are also not defined in POSIX, that doesn't mean the script isn't POSIX compliant.  The script doesn't use any shell features that are not provided by POSIX unless your /bin/sh includes a built in echo that doesn't have -e.  I wasn't aware of tcsh's echo, but I don't think tcsh could/should provide a /bin/sh as tcsh is most definitely not POSIX.

The systemd-analyze and journalctl utilities are undefined by POSIX, which means a POSIX-compliant script can call them without violating POSIX since POSIX lets you run utilities from anywhere.

However, "echo -e" is a violation of POSIX, because the echo utility is defined by POSIX: https://pubs.opengroup.org/onlinepubs/9 … /echo.html

And per its definition,

POSIX wrote:

Implementations shall not support any options.

[...]

If the first operand is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.

Hence, per POSIX,

echo -e "\nWelcome to `hostname`: `uname -o` `uname -r` (`uname -m`)\n"

is required to print something like:

-e \nWelcome to myhostname: GNU/Linux 5.10.7-arch1-1 (x86_64)\n

POSIX does provide the escape hatch "if you use the option -n, it's technically-not-an-option and you may do whatever; we throw our hands up in disgust". Furthermore, on XSI-conformant systems, you may optionally rely on it instead printing:

-e
Welcome to myhostname: GNU/Linux 5.10.7-arch1-1 (x86_64)

https://www.austingroupbugs.net/view.php?id=1222#c4375

The next (future) edition of POSIX will extend the undefined nature of -n to apply to -e as well, in a nod to the fact that this exists in the wild and will NEVER be standardized, but should be documented in order that all shall know how unreliable it is.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#3485 2021-01-17 22:24:50

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,862
Website

Re: Post your handy self made command line utilities

My Anti-POSIX version of that in Go smile
Edit: Replaced Zenity with Kdialog because I'm a KDE guy

package main

import (
	"bytes"
	"fmt"
	"log"
	"os/exec"
	"strings"
	"syscall"
	"time"
)

func main() {
	//This would typically go in .xprofile
	//Build up a status message and present it with kdialog
	welcome := bytes.NewBuffer(nil)

	// Boot info
	uname := syscall.Utsname{}
	err := syscall.Uname(&uname)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprintf(welcome, "Welcome to %s: %s %s (%s)\n",
		str(uname.Nodename[:]),
		str(uname.Sysname[:]),
		str(uname.Release[:]),
		str(uname.Machine[:]))

	fmt.Fprintln(welcome, "\nLast boot:\n----------")
	last := try("last", "reboot", "--limit", "1")
	date := strings.Fields(string(last))[4:8]
	fmt.Fprintf(welcome, "%s\n\n", strings.Join(date, " "))

	analyze := try("systemd-analyze")
	analyze = bytes.Replace(analyze, []byte("graphical"), []byte("Graphichal"), 1)
	fmt.Fprintf(welcome, "%s\n", analyze)

	// Boot errors
	fmt.Fprintln(welcome, "Boot errors:\n-------")
	critical := try("journalctl", "-r", "-b", "--no-pager", "-p", "err", "-q")
	if len(critical) == 0 {
		fmt.Fprintln(welcome, "no errors detected")
	} else {
		fmt.Fprintf(welcome, "%s\n", critical)
	}

	// Failed systemd units
	fmt.Fprintln(welcome, "Failed units:\n-------------")
	failed := try("systemctl", "--failed")
	fmt.Fprintf(welcome, "%s\n", failed)

	// Last logins
	fmt.Fprintln(welcome, "Last logins:\n------------")
	last = try("last")
	var i int
	for _, entry := range strings.Split(string(last), "\n") {
		if i == 5 {
			break
		}
		if strings.Contains(entry, "reboot") {
			continue
		}
		fields := strings.Fields(entry)
		entry = fields[0] + "\t" + strings.Join(fields[2:7], " ")
		fmt.Fprintln(welcome, entry)
		i++
	}

	fmt.Fprintln(welcome, "\nNice to see you!")

	kdialog := exec.Command("kdialog", "--textbox", "-", "1150", "800")
	kdialog.Stdin = welcome
	kdialog.Run()
}

func str(arr []int8) string {
	b := make([]byte, 0, len(arr))
	for _, v := range arr {
		if v == 0x00 {
			break
		}
		b = append(b, byte(v))
	}
	return string(b)
}

func try(name string, args ...string) (out []byte) {
	var err error
	for {
		out, err = exec.Command(name, args...).Output()
		if err == nil {
			break
		}
		time.Sleep(time.Second)
	}
	return out
}

Last edited by ugjka (2021-01-18 15:24:09)


https://ugjka.net
paru > yay | vesktop > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#3486 2021-01-18 13:43:01

Ferdinand
Member
From: Norway
Registered: 2020-01-02
Posts: 338

Re: Post your handy self made command line utilities

I have to thank you guys  I realize I'm sloppy with my scripting smile

I'm search and replacing back-ticks and echos through my brains now - and thank's for the awk tips @qinohe; I've always shunned awk, but realize it might be a tad like walking away from gold just because it's heavy tongue

Replacing g with G is sillybilly I agree, but I think the output used to be on one line? (I think I used to replace with \nGraphical)

Also I think zenity doesn't quite cut it, so I'll try with xmessage, gxmessage, xdialog or yad or something too smile

Last edited by Ferdinand (2021-01-18 15:59:48)

Offline

#3487 2021-01-18 17:31:51

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

Learn some sed and awk, they are the glue in shell scripting and can make long scripts way shorter, very valuable tools!

I think it doesn't really matter what kind of dialog you use, it's just a matter of taste and easily replaced, you could even use dmenu...

Replace the cat command for zenity , use redirection, it's a useless cat.;)
command < "$var"

Offline

#3488 2021-01-19 13:05:41

cajm
Member
From: Patagonia
Registered: 2020-12-26
Posts: 11

Re: Post your handy self made command line utilities

Here's a script I made to apply soft symlinks:

#!/bin/sh
symlink=$1
cp -r `readlink -f $symlink && rm $symlink` $symlink

C is a nice language. Semicolon.

Offline

#3489 2021-01-19 14:09:01

seth
Member
Registered: 2012-09-03
Posts: 58,693

Re: Post your handy self made command line utilities

What happens if you run that on a file that's not a symlink?
What happens if rm prints an error?
What happens if the filenames contain IFS?

Offline

#3490 2021-01-19 14:55:22

cajm
Member
From: Patagonia
Registered: 2020-12-26
Posts: 11

Re: Post your handy self made command line utilities

Then you re-write it 3 times longer:

#!/bin/bash
error () { echo "$1"; exit 1; }
symlink="$1"
test -L "$symlink" || error "$symlink is not a symlink."
test -w "$symlink" || error "No write access to $symlink"
linked=$(readlink -f "$symlink")
test -r "$linked" || error "No read access to $linked"
rm "$symlink"
cp -r "$linked" "$sym"

Last edited by cajm (2021-01-19 14:56:54)


C is a nice language. Semicolon.

Offline

#3491 2021-01-22 11:07:09

Awebb
Member
Registered: 2010-05-06
Posts: 6,602

Re: Post your handy self made command line utilities

Based on some recommendations google spit out, here is my bash script using youtube-dl that downloads Audio from Youtube and splits it into one file per video chapter.

#!/bin/bash
url="$1"
basename=$(youtube-dl --dump-single-json "$url" | jq .title | tr -cd '[:alnum:]')
downloads="~/Downloads/_youtube-dl/"
mkdir -p "$downloads"/"$basename"
cd "$downloads"/"$basename"
youtube-dl --write-info-json -x --audio-format mp3 -o "$basename.%(ext)s" "$url"
jq '.chapters[] | .start_time,.end_time-.start_time,.title ' < $basename.info.json | xargs -n3 -d'\n' | awk '{print NR" "$s}' > chapters
while read -r track start duration title; do
        title=${title//\"/}
        ffmpeg -loglevel warning -nostdin -y -ss "$start" -t "$duration" -i "$basename.mp3" -codec:a copy "$track - $title.mp3"
done < chapters

It doesn't clean up after itself and probably doesn't handle errors very well.

Offline

#3492 2021-01-22 14:36:28

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

You know youtube-dl will do all that itself with a single command.  Just use the -o flag with an output template using either the chapter name or number placeholders, and it will split chapters into separate files each named according to the template.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3493 2021-01-22 14:40:52

Awebb
Member
Registered: 2010-05-06
Posts: 6,602

Re: Post your handy self made command line utilities

That must be new. When I was looking up how to split chapters a few weeks ago, support for yt chapters were still an open bug report/feature request. I'll investigate.

Offline

#3494 2021-01-22 14:43:09

seth
Member
Registered: 2012-09-03
Posts: 58,693

Re: Post your handy self made command line utilities

There's also https://archlinux.org/packages/extra/x86_64/mp3splt/ - no chapters needed if there're reasonable silence…

Offline

#3495 2021-01-22 14:43:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

NB: I've never used it for this myself, but there is an example in the man page specifically for such a use (it separates chapters into separate directories).


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3496 2021-01-22 14:44:57

seth
Member
Registered: 2012-09-03
Posts: 58,693

Re: Post your handy self made command line utilities

Chapters (new youtube feature) or playlist items?

Offline

#3497 2021-01-22 14:46:04

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

The documentation indicates chapters, and it has a different template variable name for these chapters than it does for different playlist items.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3498 2021-01-22 16:04:42

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,862
Website

Re: Post your handy self made command line utilities

Couldn't figure out how to make systemd wait till certain zeroconf host becomes resolvable. So wrote a lil Go util that just does that and exits when there are no errors

package main

import (
        "net"
        "os"
        "time"
)

func main() {
        remote := os.Args[1]
        for {
                if _, err := net.ResolveIPAddr("ip", remote); err == nil {
                        break
                }
                time.Sleep(time.Second)
        }
}

Now in systemd unit

[Unit]
Description=extend mouse to my ircbox
Wants=network-online.target
After=network.target network-online.target

[Service]
ExecStartPre=/usr/local/bin/waitonline ircbox.local
ExecStart=/usr/bin/ssh  -X ircbox.local x2x -east -to :0.0 -nosel -completeregionup 256
Restart=always
RestartSec=5
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/ugjka/.Xauthority"

[Install]
WantedBy=default.target

https://ugjka.net
paru > yay | vesktop > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#3499 2021-01-22 16:13:23

Awebb
Member
Registered: 2010-05-06
Posts: 6,602

Re: Post your handy self made command line utilities

Trilby wrote:

The documentation indicates chapters, and it has a different template variable name for these chapters than it does for different playlist items.

Whatever those chapters are, those don't work on youtube (but on other sites). The most recent release that did anything with chapters allowed youtube-dl to download the chapter meta data in the .info.json file. It does not automatically split Youtube videos.

Offline

#3500 2021-01-23 02:09:33

karabaja4
Member
From: Croatia
Registered: 2008-09-14
Posts: 1,001
Website

Re: Post your handy self made command line utilities

Disable DPMS (screen blanking) if ALSA is currently playing sounds, or enable DPMS with 600s timeout if ALSA is currently not playing sounds.

Inspired by the fact that VLC no longer supresses DPMS and shuts my monitors off while I'm watching a movie (kernel or nvidia bug?)

Run in cron every 5 min (script is set at 10 min blanking timeout).

#!/bin/bash
# shellcheck disable=SC2155
set -euo pipefail

declare -ir _timeout=600
declare -ir _current="$(xset -display :0.0 q | awk '/Standby:/ { print $2 }')"

_enable() {
    if [[ "${_current}" == "0" ]]
    then
        xset -display :0.0 dpms ${_timeout} ${_timeout} ${_timeout}
    fi
}

_disable() {
    if [[ "${_current}" != "0" ]]
    then
        xset -display :0.0 dpms 0 0 0
    fi
}

if grep -q "RUNNING" /proc/asound/card*/pcm*/sub*/status
then
    _disable
else
    _enable
fi

Last edited by karabaja4 (2021-01-24 02:45:13)

Offline

Board footer

Powered by FluxBB