You are not logged in.

#2551 2015-03-11 20:11:02

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

Re: Post your handy self made command line utilities

uacpid reimplemented in a few lines of bash (and openbsd-netcat):

#!/bin/bash

[[ -r /run/acpid.socket ]] || exit 1

nc -U /run/acpid.socket 2>&1 |
    while read line; do
	case "$line" in
	   (...)
	esac
    done

Also, a little brain excercise:

{
  {
    cmd1 3>&- |
      cmd2 2>&3 3>&-
  } 2>&1 >&4 4>&- |
    cmd3 3>&- 4>&-

} 3>&2 4>&1

wink


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

Offline

#2552 2015-03-21 09:24:21

Quackmatic
Member
Registered: 2014-12-15
Posts: 8
Website

Re: Post your handy self made command line utilities

Here's a script I use for suspending my computer.

#!/bin/bash
POSSIBLE=$(cat /sys/power/state)
if [ $# -gt 0 ]; then
	if [[ $POSSIBLE == *"$1"* ]]; then
		echo $1 > /sys/power/state
	else
		echo "This mode is not supported on this device."
	fi
else
	echo "Usage: state [state]"
	echo "state The state to set the computer to."
	echo "      Possible values:"
	[[ $POSSIBLE == *"freeze"* ]] &&  echo "    0  freeze Suspend to idle (software-based)"
	[[ $POSSIBLE == *"standby"* ]] && echo "    1 standby Stand-by (power-on suspend)"
	[[ $POSSIBLE == *"mem"* ]] &&     echo "    3     mem Suspend to RAM (sleep)"
	[[ $POSSIBLE == *"disk"* ]] &&    echo "    4    disk Suspend to disk (hibernate)"
fi

As you can tell, I never properly learned shell scripting - I just cobble things together and hope it works - but I use this so I can just use sudo state mem and my computer suspends.


Tom.

Offline

#2553 2015-03-21 15:24:41

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

"systemctl suspend" does virtually the same thing.  It was only added to provide a suspend.target.

Offline

#2554 2015-03-23 03:12:02

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Post your handy self made command line utilities

My tmux is set up to notify me of alerts in any session. I wanted a script to use that to remind me of things at specific times. Recently I was shown a bash function which inspired this:

#!/bin/bash

# odd args are time to wake, even args are messages to display
# times starting with '@' are to display a message at that time
# otherwise, the they are the time to wait before displaying a message
# example: remindme @5:30pm 'prepare dinner' 1h 'stop working'

while (( $# ))
do [[ $1 =~ ^@ ]] && set -- "$(( $(date +%s -d "${1#@}") - $(date +%s) ))" "${@:2}"
   sleep "$1" && printf '%s  %s\a\n' "`date '+%Y/%m/%d %H:%M:%S'`" "$2" &
   shift 2
done
wait

If you want to use regular notifications, replace the printf line with something like:

   sleep "$1" && notify-send -a remindme "$2" &

(Don't forget to drop "wait". That is only useful when the output is in the terminal.)


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#2555 2015-03-23 03:46:59

Knute
Member
From: Minot, ND
Registered: 2009-03-17
Posts: 604

Re: Post your handy self made command line utilities

I use timers for alot of things, especially when I am cooking.

I have atd set up along with a notification daemon and espeak-pulse.


#!/bin/zsh
#This is dzmsg
notify-send -a "Alarm" -u critical "Notice" "$*"
mpv /home/knute/waves/spells/energy2.wav
espeak -g 25 -v en-us "Reminder $*" 

So, after writing this, I decided to set up a script to call this so all that I have to do is call the script, the time, and the message and don't have to remember the at command.

#!/bin/zsh
#This is atmsg
minutes=$1
shift 1
message=$*
echo "dzmsg $message" | at now +$minutes minutes 
xecho.sh atq

This script is called with

atmsg 5 Test message
atmsg <time> <message>

I wrote the script so that your message (the last option) can be of any length.
When it's executed, I set it up so that the record of upcoming at jobs shows up bottom center of my screen (that's the xecho.sh dzen script).
When the time is out, I get a notify via notify-send, I hear the thunder and get an audible reminder that speaks the message.

Just for convenience sake here is the xecho.sh script as well.  smile

#!/bin/sh
RESOLUTIONX=1600
RESOLUTIONY=1200
FONTWIDTH=18
FONTHEIGHT=24
FONTNAME=consolas
BORDERX=13
BORDERY=6

OPTIONS="-p 10 -ta c -sa c -fn -*-$FONTNAME-*-*-*-*-$FONTHEIGHT-*-*-*-*-*-*-* -fg yellow -bg blue -e onstart=uncollapse;button1=exit;button2=exit;button3=exit;key_Escape=exit"
OUTPUT=`mktemp`
$* >$OUTPUT

H=`wc -l <$OUTPUT`
W=`wc -L <$OUTPUT`
N=`echo $H - 1 | bc`
WIDTH=`echo  $BORDERX '+' $FONTWIDTH  '*' $W | bc`
HEIGHT=`echo $BORDERY '+' $FONTHEIGHT '*' $H | bc`
X=`echo '(' $RESOLUTIONX - $WIDTH ')'  / 2 | bc`
Y=`echo '(' $RESOLUTIONY - $HEIGHT ')'  | bc`

dzen2 -l $N -x $X -y $Y -w $WIDTH $OPTIONS <$OUTPUT
rm $OUTPUT

This xecho.sh script originally came from the dzen script site.  I changed some of the math to change the position of where the notification shows up, screen resolution and that kind of thing.

Last edited by Knute (2015-03-23 03:50:30)


Knute

Offline

#2556 2015-03-25 00:43:23

AnonFriendly
Member
Registered: 2015-02-15
Posts: 14

Re: Post your handy self made command line utilities

Hey guys, First real script, so be gentle. Just a simple mount script. I'm sure its been done before, just wanted practice. If you want more types just add t3,t4 and so on, same with devices.

#!/bin/bash
t=ext4
t2=vfat
d1=/dev/sdb1
d2=/dev/sdc1
clear
sudo fdisk -l
echo 'What type is it?'
echo 1. $t
echo 2. $t2
read a
echo 'What device is it?'
echo 1. $d1
echo 2. $d2
read b
if [ "$a" == "1" ]
then
  y=$t
fi
if [ "$a" == "2" ]
then
  y=$t2
fi
if [ "$b" == "1" ]
then
  z=$d1
  w=int
fi
if [ "$b" == "2" ]
then
  z=$d2
  w=ext
fi
sudo mount -t $y $z /mnt/$w
clear

Offline

#2557 2015-03-25 22:00:32

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Post your handy self made command line utilities

You can parse 'lsblk' output to make it more automatic and less interactive.

Offline

#2558 2015-03-26 00:07:37

AnonFriendly
Member
Registered: 2015-02-15
Posts: 14

Re: Post your handy self made command line utilities

I'm sorry, i dont understand. what do you mean?

Offline

#2559 2015-03-26 00:08:16

AnonFriendly
Member
Registered: 2015-02-15
Posts: 14

Re: Post your handy self made command line utilities

oh sorry, i understand now. great improvement, thank you

Offline

#2560 2015-04-01 10:04:30

snakeroot
Member
Registered: 2012-10-06
Posts: 168

Re: Post your handy self made command line utilities

  1. Copy the script and change PS1 in your ~/.bashrc file

    PS1='$(/path/to/script)'
  2. ?

  3. PROFIT

Script:

#!/bin/bash

declare -A assigned_letter
j=0
alf="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# for i in $(cat mtfile | grep -E -e /dev/fd[0-9] -e /dev/[hs]d[a-z][1-9][0-9]* -e /dev/cd[0-9] | cut -d" " -f 1 | uniq | sort); do
# echo $i

for i in $(/usr/bin/lsblk -nlp -o KNAME,FSTYPE | grep -v -e ' $' -e 'swap' | cut -d" " -f1); do

if [[ "${i}" = /dev/fd* ]]; then 
	if [[ ${j} -le 1 ]]; then
		assigned_letter[${i}]=${alf:j:1}
		j=$j+1
	else
		assigned_letter[${i}]="HOLD"
	fi
else
	if [[ "${j}" -lt 2 ]]; then
		j=2
	fi
	assigned_letter[${i}]=${alf:j:1}
	j=$j+1
fi
done
assigned_letter[ramdisk]=${alf:j:1}
j=$j+1
for n in ${!assigned_letter[*]}; do
	if [[ ${assigned_letter[${n}]} = HOLD ]]; then
		assigned_letter[${n}]=${alf:j:1}
	fi
done
for k in ${!assigned_letter[*]}; do
	if [[ (${k} = $(/usr/bin/df . --output=source | /usr/bin/sed 1d)) || (${k} = ramdisk && $(/usr/bin/df . --output=source | /usr/bin/sed 1d) != "/dev/*") ]]; then
		drive_letter=${assigned_letter[${k}]}
	fi
done
ps1=$(pwd | sed -e 's:.*:\U&:g' -e 's:/:\\:g') 
ps1="${drive_letter}"\:"$ps1"\>
echo $ps1

Offline

#2561 2015-04-15 19:56:44

theodoreward
Member
Registered: 2013-04-05
Posts: 20

Re: Post your handy self made command line utilities

This is a pretty handy script if you have several machines you keep updated by logging in with SSH.
It starts a shell as a background process and logs the output for each server in a separate log file, then monitors the progress line by line until all are finished (or the user presses CTRL+C).
There is also an option for checking to see if any servers need to be rebooted due to updates (existence of /var/run/reboot-required).

Caveats:
  1) If an update requires manual interaction you will have to log in and deal with the issue.
  2) I'm using certificates so no passwords required from administrative machine.
  3) My servers are all Debian so the script uses apt-get to start updates.

The output looks like this:

$ updateservers.sh -m
Monitor update logs: press CTRL+C to quit at any time to stop monitoring

HOST [0s - CTRL+C to exit]    LAST ENTRY
----------------------------  ---------------------------------------------------------------
hostname1                     Finished:  7 seconds spent updating hostname1
hostname2                     Finished: 15 seconds spent updating hostname2
hostname3                     Finished: 12 seconds spent updating hostname3

NO ACTIVE UPDATES REMAINING
#!/bin/bash

SSH="ssh"

# Disable host key checking if needed.
OPT_DISABLE_HOSTKEY_CHECK="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

MACHINES="
	hostname1
	hostname2
	hostname3"

LOGDIR="/home/tward/scripts/log/server_updates"

# Input: One or more hostnames to be updated via ssh.
# Stats update() for in the background for each hostname, 
#   channels output of update into $LOGDIR/<hostname>.log for each host.
parallel_updates()
{
	mkdir -p $LOGDIR
	until [ -z "$1" ]; do
		echo "Starting update for $1"
		update $1 > $LOGDIR/$1.log 2>&1 &
		shift
	done
}

# Input: one or more hosts to update.
# Displays information via echo.
update()
{
	until [ -z "$1" ]; do
		host=$1
		update="apt-get update && apt-get -y dist-upgrade"
		starttime=`date +%s`

		echo "Begin updating $host: " `date` 

		ssh root@$host $update
		retcode=$?
		if [ $retcode -ne 0 ]; then
			echo "Finished: UPDATE TERMINATED $host returned error code \"$retcode\""
			return
		fi
		shift
	
        	now=`date +%s`
      		let seconds=now-starttime
		echo "Finished: $seconds seconds spent updating $host"
	done
}

# Input: one or more hostnames to check for required reboot.
# Check for existence of /var/run/reboot-required on remote machine.
reboot_needed()
{
	echo
	echo -n "Checking for reboots required: "
	until [ -z "$1" ]; do
		echo -n "$1...  "
		ssh root@$1 "[ -f /var/run/reboot-required ] && echo -e '\n$1 needs rebooted!\n... '"
		shift
	done
	echo
	echo "Finished checking for reboots"
}

# Continuously monitor log files in $/LOGDIR until CTRL+C is pressed.
monitor_results()
{
    echo "Monitor update logs: press CTRL+C to quit at any time to stop monitoring"
    start=`date +%s`
    while [ 1 ]; do
        alldone=1

        now=`date +%s`
        let seconds=now-start

        printf "\nHOST [%ss - CTRL+C to exit]    LAST ENTRY\n" "$seconds"
        echo "----------------------------  ---------------------------------------------------------------"
        for i in $*; do
            line=`tail -1 $LOGDIR/$i.log`
            printf "%-30s %s\n" "$i" "$line"
            [[ "${line:0:8}" != "Finished" ]] && alldone=0
        done

        [[ $alldone -eq 1 ]] && break
        sleep 5
    done
    echo
    echo "NO ACTIVE UPDATES REMAINING"
}

function usage()
{
    [[ $# -gt 0 ]] && printf "ERROR: $*\n"
    printf "Usage: ${0##*/} ACTION
    -u:  Initiate updates and monitor progress until all are finished.
    -m:  Monitor update progress until all are finished.
    -r:  Check for required reboots.
    -h:  Display this message.\n"
    exit
}

# Process command line.
case $1 in
    -u|u )
	parallel_updates $MACHINES
	echo "Updates initiated"
	monitor_results $MACHINES
	;;
    -m|m ) monitor_results $MACHINES ;;
    -r|r ) reboot_needed $MACHINES ;;
    -[h?] ) usage ;;
    * ) usage "No action specified" ;;
esac

Last edited by theodoreward (2015-04-15 19:59:59)

Offline

#2562 2015-04-16 17:01:41

Ashren
Member
From: Denmark
Registered: 2007-06-13
Posts: 1,229
Website

Re: Post your handy self made command line utilities

.bashrc function to delete offending key in known_hosts:

function sd()  {
 sed -i ''"${1}"'d' $HOME/.ssh/known_hosts
}

Offline

#2563 2015-04-16 21:14:55

ackalker
Member
Registered: 2012-11-27
Posts: 201

Re: Post your handy self made command line utilities

Have you ever felt frustrated when your well crafted sed expression has an error and causes your script to crash and burn? Over and over again?

How about live coding your sed expressions? Here's a script that allows you to do just that.
Run it with a (test) input file as you would pass it to sed as input.
This should run your favorite editor where you can edit your sed expressions, as well as open another window showing the difference between sed's input and output when evaluating your expressions.
As soon as you save the file in your editor, the other window should update with the result of your changes.
Don't forget to save the result of your sed-fu somewhere safe, the temporary file holding the expressions will be deleted as soon as you exit the editor.

As an added bonus: if you keep the "-d" option to watch, as soon as there are changes to display, you get a nice little clock, with blinkenlights!

#!/bin/bash

# livesed - sed script live coding

: ${WATCHOPTS:="-d"}
: ${DIFFOPTS:="-U0"}

[ $# -ne 1 ] && echo "Usage: livesed <input file>" && exit 1

INFILE=$1
SCRIPT=$(mktemp)
OUTFILE=$(mktemp)

trap 'rm -f $SCRIPT $OUTFILE' EXIT

${TERMINAL:-xterm} -e watch $WATCHOPTS "sed $SEDOPTS -f $SCRIPT < $INFILE > $OUTFILE; diff $DIFFOPTS $INFILE $OUTFILE"&
${EDITOR:-vi} $SCRIPT

Enjoy smile

EDIT: Add SEDOPTS for specifying sed options, allow overriding default options from the environment.

Last edited by ackalker (2015-04-17 06:19:52)

Offline

#2564 2015-05-21 11:30:14

Chazza
Wiki Maintainer
Registered: 2013-06-02
Posts: 506

Re: Post your handy self made command line utilities

The gnome-screensaver devs removed the idle-watcher a while back meaning that gnome-screensaver will only lock the screen when idle if gnome-session is also running. This is my crude and hacky workaround: https://github.com/charlesbos/my-script … dlelock.sh

Last edited by Chazza (2015-08-28 11:29:07)

Offline

#2565 2015-06-01 17:54:20

theodoreward
Member
Registered: 2013-04-05
Posts: 20

Re: Post your handy self made command line utilities

I have a hard time remembering lots of command line things I don't use often enough.
Below is a very simple script that simply allows me to quickly view or modify my "oneliners" text files. New oneliner files can be created as well by editing (-e) a file that doesn't yet exist.

#!/bin/bash

show_help()
{
    # Get list of availble files.
    DOCS="`/usr/bin/ls /home/tward/scripts/help/`"
    DOCS=`echo $DOCS | sed "s/ /|/g"`
    
    echo "Usage: ${0##*/} <OPTION> <$DOCS>
        OPTIONs:
        -e: Edit oneliner file
        -n: No pager
        -h: Show help"
}

if [ -z $1 ]; then
    show_help
    exit
fi

CMD="less"

# If first argument is -n, change "less" to "cat".
[[ "$1" == "-n" ]] && CMD="cat" && shift
[[ "$1" == "-e" ]] && CMD="$EDITOR" && shift
[[ "$1" == "-h" ]] && CMD="show_help" && shift
$CMD /home/tward/scripts/help/$1

The oneliner files are maintained in separate text files in a given folder. In the example below you can see that the files I have are the standard awk and sed oneliner files from the interwebs, and several others that I created to remind me of commands I can't remember.

~ $ oneliners.sh 
Usage: oneliners.sh <OPTION> <awk|bash|find|git|lsof|pacman|sed>
        OPTIONS:
        -e: Edit oneliner file
        -n: No pager
        -h: Show help

~ $ oneliners.sh -n find
---- Find all hidden files in home directory.
find $HOME -name ".*" -ls

---- Exclude hidden files from results
find . -type f | grep -v '/\.'

---- Make recursion work even if no files in top folder.
egrep --color=always -r --include="$FILES" "$PATTERN" .

---- Find files of type png OR jpg
    matching: find . \( -name "*png" -o -name "*gif" \)
Non-matching: find . ! \( -name "*png" -o -name "*gif" \)

---- Only consider regular files 50M or greater
find . -type f -size +50M ! \( -name "*deb" -o -name "*vmdk" \)

---- Use regex to find all files NOT of type png OR jpg
    matching: find . -regex  '.*\(png\|gif\)$'
Non-matching: find . ! -regex  '.*\(png\|gif\)$'

---- Find files owned by a given user or group.
find <where> -user {username} 
find <where> -group {group-name} 

Last edited by theodoreward (2015-06-01 17:57:40)

Offline

#2566 2015-06-01 18:05:05

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Post your handy self made command line utilities

Don't parse ls, use globbing. And use [[ over [...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2567 2015-06-01 21:21:31

theodoreward
Member
Registered: 2013-04-05
Posts: 20

Re: Post your handy self made command line utilities

jasonwryan wrote:

Don't parse ls, use globbing. And use [[ over [...

I have never quite gotten the hang of globbing, but using the example from the page linked to I retrofitted the code as follows:

#!/bin/bash
shopt -s nullglob
DOCHOME="$HOME/scripts/help"
show_help()
{
    # Get list of available files.
    DOCS=`for f in $DOCHOME/*; do echo -n "$(basename "$f")|"; done`
    DOCS=`echo $DOCS | sed "s/|$//g"`
    
    echo "Usage: ${0##*/} <OPTION> <$DOCS>
        OPTIONs:
        -e: Edit oneliner file
        -n: No pager
        -h: Show help"
}

if [[ -z $1 ]]; then
    show_help
    exit
fi

CMD="less"

# If first argument is -n, change "less" to "cat".
[[ "$1" == "-n" ]] && CMD="cat" && shift
[[ "$1" == "-e" ]] && CMD="$EDITOR" && shift
[[ "$1" == "-h" ]] && CMD="show_help" && shift
$CMD $DOCHOME/$1

Offline

#2568 2015-06-02 20:34:51

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

Re: Post your handy self made command line utilities

So the new employer's IT department had good advice about passwords.  They say don't use passwords made from a word with odd characters, instead just use an easy to remember passphrase.

Yet the password requirements are the "standard" 8 or more characters with at least one capital letter, one lower case letter, one number, and one symbol.  And worse yet, we are required to change our passwords at least every 2 months.  The impracticality of remembering my own password under these constraints drove me to hack out a solution.  The simplest solution would be just to use a nice password generator - there are plenty of options available.  But what happens when I need to log in from some computer other than one of my own: I want to be able to "regenerate" my current password from anywhere.

So my idea is to have a simple command using utilities that would be on (or accessible from) any computer I might find myself on that will turn easily remembered passphrases into nutcase-IT-compliant passwords.  So I'm using this:

 printf "%s\xFE" $@ | base64

The base64 conversion will ensure one each of upper, lower, and number for any reasonable phrase.  The hex FE byte after each word ensures that there will be special characters in the string after the base64 conversion.

Here I used $@ as if it was in a script or function, and it could be so implemented.  But for me I just type my current passphrase in there, eg:

$ printf "%s\xFE" June is a crazy month | base64
SnVuZf5pc/5h/mNyYXp5/m1vbnRo/g==

With this, useful memorable passphrases can be converted to moron-IT-safe passwords on any system with bash and base64 (from coreutils).

EDIT: even if I'm stuck on a Win/Mac, it works on the javascript linux emulator


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

Offline

#2569 2015-06-03 09:05:08

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: Post your handy self made command line utilities

@ Trilby, hopefully no one there has access to your .bash_history file wink

Offline

#2570 2015-06-03 12:10:26

helix
Member
Registered: 2013-02-17
Posts: 180

Re: Post your handy self made command line utilities

Wonderful idea Trilby, but are "/" and "=" considered valid password characters?

Offline

#2571 2015-06-03 13:23:27

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

There is no such thing as "password characters".  Any service which limits what you can input is just bad.  (N.b. Many don't handle NUL very well because they're also bad.)

Last edited by Earnestly (2015-06-03 13:24:54)

Offline

#2572 2015-06-03 15:07:14

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

Re: Post your handy self made command line utilities

Not only are they valid, but they are members of a required set on our ridiculous system.


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

Offline

#2573 2015-06-03 17:21:34

theodoreward
Member
Registered: 2013-04-05
Posts: 20

Re: Post your handy self made command line utilities

Trilby wrote:

So the new employer's IT department had good advice about passwords.  They say don't use passwords made from a word with odd characters, instead just use an easy to remember passphrase ....

Lastpass is an excellent password manager and the passwords generated are never stored where the lastpass people can see them as all decryption is done locally. You can have it generate passwords that conform to specified criteria including things like "pronounceable".

Furthermore there is a scripting interface: Look at the AUR package "lastpass-cli".

Offline

#2574 2015-06-03 18:26:53

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

Re: Post your handy self made command line utilities

Thanks theodoreward, but that misses the important criteria of being able to log in from anywhere if/when needed without having anything special on the system.


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

Offline

#2575 2015-06-03 19:39:21

theodoreward
Member
Registered: 2013-04-05
Posts: 20

Re: Post your handy self made command line utilities

Trilby wrote:

Thanks theodoreward, but that misses the important criteria of being able to log in from anywhere if/when needed without having anything special on the system.

Good point, I'm not real good at RingTF documentation, post, instructions etc...

Offline

Board footer

Powered by FluxBB