You are not logged in.

#3776 2024-08-03 08:44:07

EISENFELD
Member
From: Germany
Registered: 2024-03-13
Posts: 27
Website

Re: Post your handy self made command line utilities

I coded a CLI tool in C. With it you can manage all systemd units very comfortable with a TUI. You can select units with up / down, hit Enter to display all status information and use the F -keys to start, stop, restart, enable, disable, mask, unmask and reload the unit. After every operation, the daemon reloads and updates the display. The compiled executable is only some kilobytes, but the source code is 1561 lines long. I don't know if I can paste the source code here.
But if you like, you can get the source and also a compiled binary on my GitHub page.

My username:Lennart1978 the tool is called "servicemaster".
Today I updated to version 1.1 and it works very well.
All you need: systemd-libs and ncurses (Most likely this is already installed)

It's always great when someone else has use for it. It was a lot of work for me, I'm just a hobby programmer, not a professional.


Ich weiß, dass ich nichts weiß !

Offline

#3777 2024-08-03 21:24:30

hnsn
Member
Registered: 2013-02-19
Posts: 4

Re: Post your handy self made command line utilities

EISENFELD wrote:

I coded a CLI tool in C. With it you can manage all systemd units very comfortable with a TUI. You can select units with up / down, hit Enter to display all status information and use the F -keys to start, stop, restart, enable, disable, mask, unmask and reload the unit. After every operation, the daemon reloads and updates the display. The compiled executable is only some kilobytes, but the source code is 1561 lines long. I don't know if I can paste the source code here.
But if you like, you can get the source and also a compiled binary on my GitHub page.

My username:Lennart1978 the tool is called "servicemaster".
Today I updated to version 1.1 and it works very well.
All you need: systemd-libs and ncurses (Most likely this is already installed)

It's always great when someone else has use for it. It was a lot of work for me, I'm just a hobby programmer, not a professional.

Found it!
https://github.com/Lennart1978/servicemaster

Offline

#3778 2024-08-10 18:08:43

EISENFELD
Member
From: Germany
Registered: 2024-03-13
Posts: 27
Website

Re: Post your handy self made command line utilities

ServiceMaster release version 1.3:

- DBus event loop: Reacts immediately to external changes to units
- Fixed some small bugs


Ich weiß, dass ich nichts weiß !

Offline

#3779 2024-09-21 21:37:36

HobbitJack
Member
From: Florida or Michigan
Registered: 2024-09-21
Posts: 6
Website

Re: Post your handy self made command line utilities

This is a comically long one-liner I keep in my .profile for PDF merging.

pdfmerge() { if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then echo "Usage: pdfmerge MERGED PDF1 PDF2"; echo "Merge PDF1 and PDF2 into MERGED"; return 0; else if [ "$#" != 3 ]; then echo "pdfmerge: bad usage"; echo "Try 'pdfmerge --help' for more information"; return 1; fi; gs -qo -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -o $@; fi }

Some of my classes require PDF submissions with both computer-generated output and scans of handwritten solutions, so I use this to quickly merge the two. It doesn't have perfect error checking, but it's just enough for me to remember how to use it when I do use it once every one or two weeks or so.
I suspect it may be wise to un-one-liner it at some point.


Astrophysicist and programmer. I like simple things.

Offline

#3780 2024-09-21 23:54:22

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

Re: Post your handy self made command line utilities

Here you go:

# original
pdfmerge() {
	if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
		echo "Usage: pdfmerge MERGED PDF1 PDF2"
		echo "Merge PDF1 and PDF2 into MERGED"
		return 0
	else
		if [ "$#" != 3 ]; then
			echo "pdfmerge: bad usage"
			echo "Try 'pdfmerge --help' for more information"
			return 1
		fi
		gs -qo -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -o $@
	fi
}

# simplified
pdfmerge() {
	if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
		echo "Usage: pdfmerge MERGED PDF1 PDF2"
		echo "Merge PDF1 and PDF2 into MERGED"
		return 0
	fi
	if [ "$#" != 3 ]; then
		echo "pdfmerge: bad usage"
		echo "Try 'pdfmerge --help' for more information"
		return 1
	fi
	gs -qo -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -o $@
}

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

Offline

#3781 2024-09-22 00:36:11

HobbitJack
Member
From: Florida or Michigan
Registered: 2024-09-21
Posts: 6
Website

Re: Post your handy self made command line utilities

You're right! With the returns there I don't need the extra else. Thanks! More proof comically long one-liners are a bad idea.


Astrophysicist and programmer. I like simple things.

Offline

#3782 2024-09-22 07:29:29

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

Re: Post your handy self made command line utilities

Lazy:

pdfmerge() {
    [ "$#" = 3 ] && echo gs -qo -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -o "$@" && return 0
    echo "Usage: pdfmerge MERGED PDF1 PDF2"; return 1
}

(though nb. the quoted "$@")

Online

#3783 2024-09-22 21:56:54

HobbitJack
Member
From: Florida or Michigan
Registered: 2024-09-21
Posts: 6
Website

Re: Post your handy self made command line utilities

I don't think quoting it would work as desired since it would treat the three items as a single argument to -o though, right? That might work anyhow but I haven't tested and I don't know if that would necessarily work.


Astrophysicist and programmer. I like simple things.

Offline

#3784 2024-09-22 22:14:07

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

Re: Post your handy self made command line utilities

it would treat the three items as a single argument to -o

Source this:

foo(){
   echo $#
}
bar(){
   foo "$@"
}

then run

bar 1 2 3
bar "1 2" 3

Then test
Source this:

foo(){
   echo $#
}
bar(){
   foo $@
}
bar 1 2 3
bar "1 2" 3

Notice a difference?

Online

#3785 2024-09-22 22:21:31

HobbitJack
Member
From: Florida or Michigan
Registered: 2024-09-21
Posts: 6
Website

Re: Post your handy self made command line utilities

Yeah, as expected when it's quoted it treats the quoted object as one, but when it's unquoted it treats it as two space separated objects.
I guess my brain isn't seeing why that is desirable behavior here.


Astrophysicist and programmer. I like simple things.

Offline

#3786 2024-09-22 22:25:11

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

Re: Post your handy self made command line utilities

"1 2" are one parameter, not two.
You want 'bar 1 2 3' to print "3" and 'bar "1 2" 3' to print '2'
Do you get that when not quoting $@ ?

The relevant condition is if the filenames you're passing contain spaces.

Online

#3787 2024-09-22 22:27:06

HobbitJack
Member
From: Florida or Michigan
Registered: 2024-09-21
Posts: 6
Website

Re: Post your handy self made command line utilities

Ah, that's why I didn't think of it, I tend not to use spaces in filenames. Thanks. O7


Astrophysicist and programmer. I like simple things.

Offline

#3788 2024-09-25 10:10:09

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

Re: Post your handy self made command line utilities

Using QEMU/KVM a lot recently to test ISO images so I wrote these two functions for my shell RC file:

function vinst {
   if [ $# != 2 ] ; then
      echo 'Usage vinst $iso $disk'
   else
      qemu-system-x86_64 \
         -enable-kvm \
         -m 4G \
         -device virtio-vga-gl \
         -display sdl,gl=on \
         -machine q35 \
         -device intel-iommu \
         -cpu host \
         -audiodev alsa,id=snd0 \
         -device ich9-intel-hda \
         -device hda-output,audiodev=snd0 \
         -drive if=virtio,format=raw,cache=none,file="$2" \
         -nic user,model=virtio-net-pci \
         -cdrom "$1" \
         -boot d
   fi
}

function vrun {
   qemu-system-x86_64 \
      -enable-kvm \
      -m 4G \
      -device virtio-vga-gl \
      -display sdl,gl=on \
      -machine q35 \
      -device intel-iommu \
      -cpu host \
      -audiodev alsa,id=snd0 \
      -device ich9-intel-hda \
      -device hda-output,audiodev=snd0 \
      -drive if=virtio,format=raw,cache=none,file="$1" \
      -nic user,model=virtio-net-pci
}

`vinst` is for testing live ISO images and installing, it accepts the ISO image name and the target disk image as arguments, `vrun` is for running the installed system and accepts the disk image name.

Last edited by Head_on_a_Stick (2024-09-25 10:10:40)


Para todos todo, para nosotros nada

Offline

#3789 2024-09-25 12:42:52

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

Re: Post your handy self made command line utilities

Just as a few posts before, that'd be cleaner without the else block (I also replaced the string comparison with numeric comparison:

vinst() {
   if [ $# -ne 2 ]; then
      echo 'Usage vinst <iso> <disk>'
      return 1
   fi
   qemu-system-x86_64 \
      # ...
}

Last edited by Trilby (2024-09-25 13:21:30)


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

Offline

#3790 2024-09-25 13:01:35

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

Re: Post your handy self made command line utilities

^ Thanks!

EDIT: these boards have an excellent linting service big_smile

Last edited by Head_on_a_Stick (2024-09-25 13:05:10)


Para todos todo, para nosotros nada

Offline

#3791 2024-10-01 02:45:05

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

Re: Post your handy self made command line utilities

My pdf management script

#!/bin/bash
## read below file for details of qpdf usage
# /usr/share/doc/qpdf/qpdf-manual.pdf
#
#
# TRY PDFTRICKS for gui

echo -n "
choose option
    m) merge pdf files
    e) extract pages from pdf
    p) image to pdf
    l) libreoffice document to pdf
    v) djvu to pdf
    d) remove password (decrypt)
    b) get and edit  metadata ( including bookmarks )
    r) rotate pages
    s) optimize for size
    x) exit
#?  "
    # d) delete pages from pdf (untested)
read ops

case $ops in

m)  echo "which files to merge?"
	read -a IN
	echo -n "provide name for output file:"
	read OUT
	# pdfunite ${IN[@]} $OUT.pdf
	pdftk ${IN[@]} cat output $OUT.pdf
    ;;

p)  echo "which image files to convert? e.g., *.jpg"
	read -a IN
	echo -n "provide name for output file:"
	read OUT
#	convert "${IN[@]}" $OUT.pdf
	img2pdf --output $OUT.pdf ${IN[@]}
	;;

l)  echo "which document files to convert? e.g., *.odt"
	read -a IN
#	echo -n "provide name for output file:"
#	read OUT
	libreoffice --headless --convert-to pdf "${IN[@]}"
	;;

v) echo "which djvu file to convert?"
    read -a IN
    OUT="`basename $IN .djvu`"
    OUT="$OUT".pdf
    ddjvu -format=pdf -quality=85 -verbose $IN $OUT ;;
s)
    echo "which pdf file to modify?"
	read -a IN
	echo -n "provide name for output file:"
	read OUT
#   ebook=150dpi, screen=72dpi, printer=300dpi, default=largesize
    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$OUT" "$IN"
    ;;

e)  echo -n "name the pdf file to extract images from:"
	read IN
	echo -n "first page to extract:"
	read FIRST
	echo -n "last page to extract:"
	read LAST
	echo -n "provide name for output file:"
	read OUT
	# pdfseparate -f $FIRST -l $LAST $IN $OUT%d
    pdftk $IN cat $FIRST-$LAST output $OUT.pdf
    # pdfunite $(echo $OUT*) $OUT.pdf
	;;

r)  echo -n "name the pdf file to rotate pages:"
	read IN
    echo -n "angle of rotation, +90 -90" # left,right,north,south etc with pdftk
    read ANGLE
	echo -n "first page to rotate:"
	read FIRST
	echo -n "last page to rotate:"
	read LAST
	echo -n "provide name for output file:"
	read OUT
	qpdf --rotate=$ANGLE:$FIRST-$LAST $IN $OUT.pdf
#    pdftk $IN rotate $FIRST-$LAST$ANGLE output $OUT.pdf
	;;

d)  echo -n "Which pdf file to decrypt (without .pdf) ?"
        read FILE
        echo -n "Original password ?"
        read PASS
        qpdf --password="$PASS" --decrypt $FILE.pdf $FILE.decrypted.pdf
#        pdftk $FILE.pdf input_pw "$PASS" output $FILE.decrypted.pdf
        ;;

b)  echo -n "Which pdf file (without .pdf) ?"
        read FILE
        pdftk $FILE.pdf dump_data output $FILE.txt
        echo "metadata dumped to $FILE.txt"
        echo "kindly edit the metadata and enter y to proceed"
        read reply
        [[ $reply = y ]] && pdftk $FILE.pdf update_info $FILE.txt output $FILE.bookmarked.pdf
        ;;

# d) echo " example command to remove page 21
        # pdftk $FILE.pdf cat 1-20 22-end $FILE_edited.pdf" ;;
x)	exit
	;;
esac

Offline

Board footer

Powered by FluxBB