You are not logged in.

#3576 2021-08-03 03:09:53

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,334

Re: Post your handy self made command line utilities

added all improvements suggested, here is the updated script

#!/bin/bash
shopt -s lastpipe
## make temporary file and copy all commands to it
f=$(mktemp /tmp/"${0##*/}".XXXXX)
cat $HOME/.config/shortcuts <(IFS=:; find $PATH  -executable -printf "%f\n" | sort -r ) | awk '!NF || !x[$0]++' > "$f"

## choose command to execute using fzf (if run from terminal) or rofi (if run using window-manager shortcut)
if [[ $TERM = linux && -n $DISPLAY ]]; then
	 rofi -dmenu -font "Noto Sans Mono Medium 18" -p "RUN THIS" -input $f | read command
else
	fzf --border --no-sort --reverse --height 10 --prompt "RUN THIS: " < $f | read command 
fi
## cleanup at exit
clean_f() {
	[[ -f "$f" ]] && rm "$f"
}
trap clean_f EXIT
## run the command
exec $command &

EDIT: I am happy to use cat for it's intended purpose (concatenate).
EDIT2 : removed "-type f" option from find command line, to allow symlinks like libreoffice to become available.

Last edited by Docbroke (2021-08-03 10:34:26)


Arch is home!
cwm rofi weaver vifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

#3577 2021-08-03 14:33:30

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

Re: Post your handy self made command line utilities

Docbroke wrote:

EDIT: I am happy to use cat for it's intended purpose (concatenate).

Still, you don't have to use 'cat'..;)

p="$HOME"/.config/shortcuts
IFS=:; find $PATH $p -executable -printf "%f\n" | sort -r | awk '!NF || !x[$0]++' > "$f"

Offline

#3578 2021-08-03 15:32:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 25,629
Website

Re: Post your handy self made command line utilities

I'm not clear on what that awk script does either.  Isn't that just enforcing unique matches?  Sort can do that with the -u flag.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3579 2021-08-03 15:44:44

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

Re: Post your handy self made command line utilities

@Trilby, the awk script removes duplicates.
I didn't think about sort -u but that will work too.

Offline

#3580 2021-08-03 16:01:40

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,334

Re: Post your handy self made command line utilities

qinohe wrote:

Still, you don't have to use 'cat'..;)

p="$HOME"/.config/shortcuts
IFS=:; find $PATH $p -executable -printf "%f\n" | sort -r | awk '!NF || !x[$0]++' > "$f"

You missed ! I am combining two files with cat. One is my personal shortcuts (in .confing/shortcuts) and two is commands from $PATH.

Trilby wrote:

I'm not clear on what that awk script does either.  Isn't that just enforcing unique matches?  Sort can do that with the -u flag.

I am using sort with $PATH, but I don't want to sort after joining $PATH with my shortcuts (I need them on top). As a compromise I can have

cat $HOME/.config/shortcuts <(IFS=:; find $PATH -executable -printf "%f\n" | sort -ur ) > "$f"

Arch is home!
cwm rofi weaver vifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

#3581 2021-08-03 16:14:32

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

Re: Post your handy self made command line utilities

Docbroke wrote:
qinohe wrote:

Still, you don't have to use 'cat'..;)

p="$HOME"/.config/shortcuts
IFS=:; find $PATH $p -executable -printf "%f\n" | sort -r | awk '!NF || !x[$0]++' > "$f"

You missed ! I am combining two files with cat. One is my personal shortcuts (in .confing/shortcuts) and two is commands from $PATH.

Look again, 'p' is you shortcut file, 'find $PATH $p...> "$f"' will find both paths and store it in '$f' == one sorted list??

edit: forget what I said, you're right if 'shortcuts' is a file, I was thinking 'directory'...

Last edited by qinohe (2021-08-03 16:20:56)

Offline

#3582 2021-08-03 16:52:03

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,334

Re: Post your handy self made command line utilities

Yes, I missed $p in your code, but it didn't make a sense as shortcuts is a file, with commands listed one per line. Why would I use cat with a directory ? Additionally there is no $p in updated script I posted above.


Arch is home!
cwm rofi weaver vifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

#3583 2021-08-03 19:05:59

seth
Member
Registered: 2012-09-03
Posts: 23,295

Re: Post your handy self made command line utilities

Use a better shell tongue

#!/bin/zsh
sort < somefile < somefile

More importantly though: why are you not using stest and a cache?

IFS=:
if stest -dqr -n "$cache" $PATH; then
   stest -flx $PATH | sort -u > "$cache"
fi

Offline

#3584 2021-08-03 19:27:04

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,334

Re: Post your handy self made command line utilities

That is part of dmenu_path code ? right!
I tried to use stest initially, but it needs dmenu to be installed ( as it is part of dmenu package). So I instead used find. I know it is not very logical but I don't use dmenu (as I prefer rofi), and I didn't like to keep full package installed for part of it's code.
Someday I may move to dash or ash from bash, but zsh is in opposite direction. For me leaner is better.

Last edited by Docbroke (2021-08-03 19:28:36)


Arch is home!
cwm rofi weaver vifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

#3585 2021-08-03 20:06:46

seth
Member
Registered: 2012-09-03
Posts: 23,295

Re: Post your handy self made command line utilities

Yup, comes in handy regardless.
You could map it to find and s till use a cache depending on the age of the elements in $PATH (possibly also compare $PATH to the last invocation)

Offline

#3586 2021-08-03 21:07:35

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

Re: Post your handy self made command line utilities

seth wrote:

Yup, comes in handy regardless.

If you really don't like dmenu installed there is stest in AUR.
I am with seth here and agree it's useful.

BTW. glanced over that code again, and, the cat can be removed by a bashism, I just think that cat is ugly, sorry ;-(

p=$(<"$HOME"/.config/shortcuts)
IFS=:; find $PATH $p -executable -printf "%f\n" | sort -ru

Offline

#3587 2021-08-03 21:40:27

seth
Member
Registered: 2012-09-03
Posts: 23,295

Re: Post your handy self made command line utilities

I assume .config/shortcuts contains a list of the completions he wants (shell aliases or functions?), not some paths.
So you must pipe the contents of that file and the result of the find into sort.

Offline

#3588 2021-08-03 22:18:44

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

Re: Post your handy self made command line utilities

seth wrote:

I assume .config/shortcuts contains a list of the completions he wants (shell aliases or functions?), not some paths.
So you must pipe the contents of that file and the result of the find into sort.

Yes, I understand, than there would still be a workaround needed for that alias will not be found.
I think the tool is okay but a lot of apps in the list will not work so you kinda need to know what will work and what wont, using a terminal or rofi etc...

Offline

#3589 2021-08-04 02:07:57

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,334

Re: Post your handy self made command line utilities

seth wrote:

I assume .config/shortcuts contains a list of the completions he wants (shell aliases or functions?), not some paths.

Yes indeed. Here are some selected lines from shortcuts. @qinohe, this allows me to run commands that otherwise would have failed when run  using rofi.

w3search
terminator --geometry=800x400 -x openfile
terminator --geometry=900x1000 -x vifm
terminator --geometry=700x400  -x pasd
terminator -x pase
terminator --geometry=300x350 -x otp
terminator --geometry=700x1000 -x nnn -Rex 
terminator --geometry=900x550 -x calrem
terminator --geometry=900x1000 -x htop
link-handle
vncviewer -Fullscreen -LowColorLevel 0 piserver
weaver //target/0 //profile/gmail mail.google.com

and here are some of the scripts used

┌─[sharad] (07:30 AM)-(Wed Aug 04) [~]
└───▶ cat bin/pasd
#!/bin/sh

cd $HOME/dotfiles/config/.config/password-store

#gpg -d $(fzf)

file=$(find . -type f | fzf --border --reverse --height 10 --prompt "View Password: ")
pass ${file%.gpg} && sleep 60

┌─[sharad] (07:30 AM)-(Wed Aug 04) [~]
└───▶ cat bin/openfile
#!/bin/sh

exec opener "$(find . -type f | fzf --border --reverse --height 10 --prompt "Open File : ")"

┌─[sharad] (07:30 AM)-(Wed Aug 04) [~]
└───▶ cat bin/calrem
#!/bin/sh

rem -cu+2
sleep 60

opener is mime-type based file opener written in bash, here is the sample code, actual file is longer as it contains many mimetype associations as well as some file suffix based associations. I am still in the process of improving it's code.

└───▶ cat bin/opener
#!/usr/bin/env bash

export TERM=terminator

[[ -z $1 || ! -e $1 ]] &&
    exit 1

#mime_type="$(mimetype -biL "$1")"
mime_type="$(file -b --mime-type "$1")"

case "$mime_type" in
	text/html|application/x-mimearchive)
		weaver "$@" || vimb "$@" || chromium "$@" || firefox "$@" || $TERM -x w3m "$@"
	;;

    text/*|*/xml|application/x-httpd-php3|application/x-httpd-php4|application/x-httpd-php5|application/x-shellscript)
		$TERM -x "${VISUAL:-${EDITOR:-vis}}" "$@"
    ;;
esac

Last edited by Docbroke (2021-08-04 02:11:59)


Arch is home!
cwm rofi weaver vifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

#3590 2021-08-04 05:48:01

seth
Member
Registered: 2012-09-03
Posts: 23,295

Offline

#3591 2021-08-04 07:35:45

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,334

Re: Post your handy self made command line utilities

xdg-open has limitations ( or so I believe). It needs desktop files to open apps, and it works with mimetypes only. With my code I am free to use command names without creating desktop files and I can use regular expressions. For example .rar and .cbr are both "application/x-rar", but I use unarchiver for .rar and mupdf/foliate for .cbr/.cbz


Arch is home!
cwm rofi weaver vifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

#3592 2021-08-04 15:48:02

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

Re: Post your handy self made command line utilities

Docbroke wrote:

@qinohe, this allows me to run commands that otherwise would have failed when run  using rofi.
...

Makes sense, thanks, for explaining, now I see the why;)

Offline

#3593 2021-08-06 03:23:33

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,334

Re: Post your handy self made command line utilities

I have posted file opener script on github, that works similar to xdg-open (hinted above), https://github.com/Docbroke/opener


Arch is home!
cwm rofi weaver vifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

#3594 2021-08-09 13:03:34

yarbelk
Member
Registered: 2021-08-09
Posts: 1

Re: Post your handy self made command line utilities

grdb

which is a partial implementation of a pipe based relational database system

so, to find all people named "John Doe", whow's birthday falls on a holiday; you'd do something like this.

cat input.tsv | rows Name eq "John Doe" | columns Name Birthday Address | jointbl col.Birthday=col.Holidays main_table

well.  the joins are the next thing i'm implementing.  and there are some utility functions (like adding headers automatically and/or interactively) that I want to do.

Offline

#3595 2021-08-09 13:14:57

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,222
Website

Re: Post your handy self made command line utilities

yarbelk wrote:
cat input.tsv | rows Name eq "John Doe" | columns Name Birthday Address | jointbl col.Birthday=col.Holidays main_table

Useless use of cat?!
Or: Why doesn't rows take a file as argument?

Offline

#3596 2021-08-09 13:20:03

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 25,629
Website

Re: Post your handy self made command line utilities

Why not juse use sqlite?  What benefit does the pipes system give over the following?  You most likely have sqlite already installed.

sqlite3 <<EOF
.headers on
.mode tabs
.import input.tsv people
select Name, Birthday, Address from people
join main_table on people.Birthday = main_table.Holidays
where Name = 'John Doe'
EOF

(Note, the ".headers on" and ".mode tabs" can be specified in your config file so they'd not even be needed if that's how you wanted to use it).

Last edited by Trilby (2021-08-09 13:22:40)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3597 2021-08-09 13:48:13

Awebb
Member
Registered: 2010-05-06
Posts: 5,850

Re: Post your handy self made command line utilities

I've had a look at the gitlab page and the oldest commit I could find wasn't even a week old. Looks like an early WIP and I'd give it a lot of slack regarding file inputs and cat abuse. I don't see a benefit over sqlite either, but I wouldn't whip out go to replace awk/grep/sed/cut either.

Offline

#3598 2021-08-11 03:32:05

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 25,629
Website

Re: Post your handy self made command line utilities

Transpose a data table in pure sed.  Pass a file name, and optionally a delimeter character (as argv[2]) which defaults to tab:

#!/bin/sh

s=${2:-\\t}

sed -n -f /dev/stdin <<EOF "$1"
{ H; }
\$ {
	g; s/^\n//
	:loop
		h; s/$s[^\n]*/$s/g; s/\n//g; s/$s\$//; p
		g; s/[^$s]*$s\([^\n]*\)\n*/\1\n/g
		/$s/ b loop
	s/\n/$s/g; s/$s\$//; p
}
EOF

(developed here)

Last edited by Trilby (2021-08-11 03:32:32)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3599 2021-08-11 12:19:47

karabaja4
Member
From: Croatia
Registered: 2008-09-14
Posts: 928

Re: Post your handy self made command line utilities

Docbroke wrote:

xdg-open has limitations ( or so I believe). It needs desktop files to open apps, and it works with mimetypes only. With my code I am free to use command names without creating desktop files and I can use regular expressions. For example .rar and .cbr are both "application/x-rar", but I use unarchiver for .rar and mupdf/foliate for .cbr/.cbz

xdg-open is so horrible. The insistence on stupid desktop files to run simple commands, and inability to use globs or regexes for mime types - for example it makes sense to assign "video/*" to VLC (which could potentially be done inside mimeapps.list using globs), but xdg-open does not enable this but instead opts to map every single video mime type to a desktop file.

I did something similar to your script in Node to replace xdg-open: https://github.com/karabaja4/mimejs

Last edited by karabaja4 (2021-08-11 13:57:24)

Offline

#3600 2021-08-11 12:36:19

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,215
Website

Re: Post your handy self made command line utilities

There's a high variety of xdg-open "replacements", most of which offer incompatible behavior. See the table here: https://wiki.archlinux.org/title/Defaul … ce_openers


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

Board footer

Powered by FluxBB