You are not logged in.
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
Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
Offline
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
"systemctl suspend" does virtually the same thing. It was only added to provide a suspend.target.
Offline
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
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.
#!/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
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
You can parse 'lsblk' output to make it more automatic and less interactive.
Offline
I'm sorry, i dont understand. what do you mean?
Offline
oh sorry, i understand now. great improvement, thank you
Offline
Copy the script and change PS1 in your ~/.bashrc file
PS1='$(/path/to/script)'
?
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
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
.bashrc function to delete offending key in known_hosts:
function sd() {
sed -i ''"${1}"'d' $HOME/.ssh/known_hosts
}
Offline
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
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
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
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
Don't parse ls, use globbing. And use [[ over [...
Offline
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
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
@ Trilby, hopefully no one there has access to your .bash_history file
Offline
Wonderful idea Trilby, but are "/" and "=" considered valid password characters?
Offline
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
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
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
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
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