You are not logged in.

#1901 2012-08-24 16:52:03

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

Re: Post your handy self made command line utilities

Total, blank, non-blank:

wc -l < $filename
grep -c '^$' $filename
grep -cv '^$' $filename

Last edited by karol (2012-08-24 16:52:49)

Offline

#1902 2012-08-24 16:53:55

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

Re: Post your handy self made command line utilities

You should probably check out `wc`, specifically `wc -l` to get the total lines, and

grep -v "^ *$" filename | wc -l

to get a nonblank line count.

Edit: too slow ... but I just learned about grep's -c option. Cool.

Last edited by Trilby (2012-08-24 16:55:17)


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

Offline

#1903 2012-08-24 17:37:41

wuyingren
Member
From: Catalunya
Registered: 2009-03-18
Posts: 8
Website

Re: Post your handy self made command line utilities

Damn, @karol is right

The fact is that i wanted the script to detect CRLF also, but then I discovered dos2unix....and then it seems I forgot about wc... : (

Anyway, it was good for my bash learning : P

Offline

#1904 2012-08-26 01:30:35

pilotkeller
Member
From: Canada
Registered: 2011-12-22
Posts: 513
Website

Re: Post your handy self made command line utilities

This is a beautiful way I run Radio Paradise. Though, most other internet radio stations would work with a simple URL switch.

#!/bin/zsh

kill -9 `ps x | grep "mplayer -msglevel all=-1 -noconsolecontrols -nojoystick -nolirc -nomouseinput http://stream-sd.radioparadise.com:9000/rp_32.ogg" | grep -v "grep" | cut -f 2 -d\ ` 2>/dev/null
if [ $? = 1 ]; then
    mplayer -msglevel all=-1 -noconsolecontrols -nojoystick -nolirc -nomouseinput http://stream-sd.radioparadise.com:9000/rp_32.ogg &
fi

Last edited by pilotkeller (2012-08-26 01:47:42)

Offline

#1905 2012-08-26 11:25:31

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

Re: Post your handy self made command line utilities

pilotkeller wrote:

This is a beautiful way I run Radio Paradise. Though, most other internet radio stations would work with a simple URL switch.

#!/bin/zsh

kill -9 `ps x | grep "mplayer -msglevel all=-1 -noconsolecontrols -nojoystick -nolirc -nomouseinput http://stream-sd.radioparadise.com:9000/rp_32.ogg" | grep -v "grep" | cut -f 2 -d\ ` 2>/dev/null
if [ $? = 1 ]; then
    mplayer -msglevel all=-1 -noconsolecontrols -nojoystick -nolirc -nomouseinput http://stream-sd.radioparadise.com:9000/rp_32.ogg &
fi

Why not use  'pkill mplayer' to kill it?

Offline

#1906 2012-08-26 12:22:43

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

Re: Post your handy self made command line utilities

Because one never knows what instance pkill kills?

Offline

#1907 2012-08-26 12:42:51

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

Re: Post your handy self made command line utilities

Awebb wrote:

Because one never knows what instance pkill kills?

Not sure if I get what you mean, but would

kill -9 $(pgrep -fo radioparadise)

be better?

Offline

#1908 2012-08-26 13:01:03

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

Maybe somethinng like:

 { mplayer ... & pid=$!; }; wait; kill $pid 

Offline

#1909 2012-08-26 16:54:53

pilotkeller
Member
From: Canada
Registered: 2011-12-22
Posts: 513
Website

Re: Post your handy self made command line utilities

karol wrote:

Why not use  'pkill mplayer' to kill it?

Because I often watch lectures and then pause them as my mind hits the wall. This would kill them.

karol wrote:
kill -9 $(pgrep -fo radioparadise)

Problem is that mplayer threads into two for web streams so the script would run twice before it worked as intended.

steve___ wrote:
{ mplayer ... & pid=$!; }; wait; kill $pid 

And this is why I wrote the script. The content is a stream, so it never exits. lol

MISTAKE: cut -f 2 should be cut -f 1 -- This came from starting with ps aux but changed it to limit entries.

Last edited by pilotkeller (2012-08-26 17:03:38)

Offline

#1910 2012-08-26 17:17:52

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

Re: Post your handy self made command line utilities

pilotkeller wrote:
karol wrote:
kill -9 $(pgrep -fo radioparadise)

Problem is that mplayer threads into two for web streams

I know.

pilotkeller wrote:

so the script would run twice before it worked as intended.

Seems to work for me - I just need to run it once and 'kill' works w/o the '-9' flag too:

kill $(pgrep -fo radioparadise)

Offline

#1911 2012-08-26 17:30:49

pilotkeller
Member
From: Canada
Registered: 2011-12-22
Posts: 513
Website

Re: Post your handy self made command line utilities

karol wrote:

Seems to work for me - I just need to run it once and 'kill' works w/o the '-9' flag too:

kill $(pgrep -fo radioparadise)

Lions, and Tigers, and Shell, OH MY!

When you send SIGTERM vs SIGKILL it stops both! yikes

#!/bin/zsh

kill $(pgrep -fo radioparadise) 2>/dev/null
if [ $? = 1 ]; then
    mplayer -msglevel all=-1 -noconsolecontrols -nojoystick -nolirc -nomouseinput http://stream-sd.radioparadise.com:9000/rp_32.ogg &
fi

Thanks karol! <3

Offline

#1912 2012-08-26 17:31:57

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

@Karol: The problem is any 'radioparadise' will match.  Eg:

$ vim radioparadise
$ pgrep -fo radioparadise

Last edited by steve___ (2012-08-26 17:32:34)

Offline

#1913 2012-08-26 17:36:19

pilotkeller
Member
From: Canada
Registered: 2011-12-22
Posts: 513
Website

Re: Post your handy self made command line utilities

If there is that much worry just use

#!/bin/zsh

stream_url="http://stream-sd.radioparadise.com:9000/rp_32.ogg"

kill $(pgrep -fo $stream_url) 2>/dev/null
if [ $? = 1 ]; then
    mplayer -msglevel all=-1 -noconsolecontrols -nojoystick -nolirc -nomouseinput $stream_url &
fi

Last edited by pilotkeller (2012-08-26 17:42:40)

Offline

#1914 2012-08-26 17:37:20

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

Re: Post your handy self made command line utilities

steve___ wrote:

@Karol: The problem is any 'radioparadise' will match.  Eg:

$ vim radioparadise
$ pgrep -fo radioparadise

Sure, but e.g.

kill $(pgrep -fo stream-sd.radioparadise)

is less likely to cause problems.

Edit: Too late ;P


pilotkeller wrote:

When you send SIGTERM vs SIGKILL it stops both! yikes

The '-o' flag of pgrep acts against the oldest process, otherwise you get an error about the other (newer) process being already gone:

$ pgrep -f stream-sd.radioparadise
1946
1947
$ kill $(pgrep -f stream-sd.radioparadise)
bash: kill: (1947) - No such process

Last edited by karol (2012-08-26 17:42:57)

Offline

#1915 2012-08-26 17:46:55

pilotkeller
Member
From: Canada
Registered: 2011-12-22
Posts: 513
Website

Re: Post your handy self made command line utilities

karol wrote:
pilotkeller wrote:

When you send SIGTERM vs SIGKILL it stops both [threads]! :O

No no. When you run plain kill it sends a SIGTERM. The -9 makes it a SIGKILL. Although a SIGTERM is always prefered, many programs don't handle SIGTERMs so SIGKILL is what gets used by habbit. lol

Edit: Probably my bad for the :O. The icon makes it more paniced then shocked. lol

Last edited by pilotkeller (2012-08-26 17:54:27)

Offline

#1916 2012-08-26 19:33:58

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

Re: Post your handy self made command line utilities

Ah, now I get what you meant.

BTW,

pkill -f "http://stream-sd.radioparadise.com:9000/rp_32.ogg"

works too ;P

Offline

#1917 2012-08-29 16:11:06

brenix
Member
From: California
Registered: 2008-03-05
Posts: 185

Re: Post your handy self made command line utilities

Here is a basic script I wrote in just a few minutes to take a xresources colorscheme and print out a reg file to import in windows for use with putty/kitty. By default, it sets the registry path for use with Kitty, but I put another line in there that can be un-commented to use with putty. Could be improved, but I thought I'd share so others can make it better!

Usage: Pass a .Xdefaults file or file containing colors
Example:

> xcolorstoreg.sh .Xdefaults
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\9bis.com\KiTTY\Sessions\Default%20Settings]
"Colour0"="220,220,204"
"Colour1"="220,220,204"
"Colour2"="58,58,58"
"Colour3"="58,58,58"
"Colour4"="220,220,204"
"Colour5"="220,220,204"
"Colour6"="30,35,32"
"Colour7"="112,144,128"
"Colour8"="112,80,80"
"Colour9"="220,163,163"
"Colour10"="96,180,138"
"Colour11"="195,191,159"
"Colour12"="223,175,143"
"Colour13"="240,223,175"
"Colour14"="80,96,112"
"Colour15"="148,191,243"
"Colour16"="220,140,195"
"Colour17"="236,147,211"
"Colour18"="140,208,211"
"Colour19"="147,224,227"
"Colour20"="220,220,204"
"Colour21"="255,255,255"

Source:

#!/bin/bash

xfile=$1
fallbackbg="0,0,0"
fallbackfg="170,170,170"

function hex2rgb () {
        hexinput=$1
        a=`echo $hexinput | cut -c-2`
        b=`echo $hexinput | cut -c3-4`
        c=`echo $hexinput | cut -c5-6`
        r=`echo "ibase=16; $a" | bc`
        g=`echo "ibase=16; $b" | bc`
        b=`echo "ibase=16; $c" | bc`
        echo "$r,$g,$b"
}

while read line
do
        colorname=$(echo $line | tr [:lower:] [:upper:] | sed 's/:.*//g' | sed 's/.*\*//g')
        hexcolor=$(echo $line | tr [:lower:] [:upper:] | sed 's/.*#//g')
        case "$colorname" in
                FOREGROUND)
                        foreground=$(hex2rgb $hexcolor)
                        ;;
                BACKGROUND)
                        background=$(hex2rgb $hexcolor)
                        ;;
                COLOR0)
                        black0=$(hex2rgb $hexcolor)
                        ;;
                COLOR8)
                        black1=$(hex2rgb $hexcolor)
                        ;;
                COLOR1)
                        red0=$(hex2rgb $hexcolor)
                        ;;
                COLOR9)
                        red1=$(hex2rgb $hexcolor)
                        ;;
                COLOR2)
                        green0=$(hex2rgb $hexcolor)
                        ;;
                COLOR10)
                        green1=$(hex2rgb $hexcolor)
                        ;;
                COLOR3)
                        yellow0=$(hex2rgb $hexcolor)
                        ;;
                COLOR11)
                        yellow1=$(hex2rgb $hexcolor)
                        ;;
                COLOR4)
                        blue0=$(hex2rgb $hexcolor)
                        ;;
                COLOR12)
                        blue1=$(hex2rgb $hexcolor)
                        ;;
                COLOR5)
                        magenta0=$(hex2rgb $hexcolor)
                        ;;
                COLOR13)
                        magenta1=$(hex2rgb $hexcolor)
                        ;;
                COLOR6)
                        cyan0=$(hex2rgb $hexcolor)
                        ;;
                COLOR14)
                        cyan1=$(hex2rgb $hexcolor)
                        ;;
                COLOR7)
                        white0=$(hex2rgb $hexcolor)
                        ;;
                COLOR15)
                        white1=$(hex2rgb $hexcolor)
                        ;;
                *)
                        echo "WTF!" > /dev/null
                        ;;
        esac
done < $xfile

if [ -z "$foreground" ]; then
        foreground=$fallbackfg
fi

if [ -z "$background" ]; then
        background=$fallbackbg
fi

echo "Windows Registry Editor Version 5.00"
echo ""
# FOR USE WITH PUTTY
#echo "[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\Default%20Settings]"
# FOR USE WITH KITTY
echo "[HKEY_CURRENT_USER\Software\9bis.com\KiTTY\Sessions\Default%20Settings]"
echo -e "\"Colour0\"=\"$foreground\""
echo -e "\"Colour1\"=\"$foreground\""
echo -e "\"Colour2\"=\"$background\""
echo -e "\"Colour3\"=\"$background\""
echo -e "\"Colour4\"=\"$foreground\""
echo -e "\"Colour5\"=\"$foreground\""
echo -e "\"Colour6\"=\"$black0\""
echo -e "\"Colour7\"=\"$black1\""
echo -e "\"Colour8\"=\"$red0\""
echo -e "\"Colour9\"=\"$red1\""
echo -e "\"Colour10\"=\"$green0\""
echo -e "\"Colour11\"=\"$green1\""
echo -e "\"Colour12\"=\"$yellow0\""
echo -e "\"Colour13\"=\"$yellow1\""
echo -e "\"Colour14\"=\"$blue0\""
echo -e "\"Colour15\"=\"$blue1\""
echo -e "\"Colour16\"=\"$magenta0\""
echo -e "\"Colour17\"=\"$magenta1\""
echo -e "\"Colour18\"=\"$cyan0\""
echo -e "\"Colour19\"=\"$cyan1\""
echo -e "\"Colour20\"=\"$white0\""
echo -e "\"Colour21\"=\"$white1\""

Last edited by brenix (2012-08-29 16:29:08)

Offline

#1918 2012-08-29 16:33:43

Shark
Member
From: /dev/zero
Registered: 2011-02-28
Posts: 686

Re: Post your handy self made command line utilities

I use dropbox for storing some backups. This is a script i have made to fully automate it. I use gpg encryption of tar file too when uploading to dropbox.

#!/bin/bash                                                                                     
                                                                                                
echo "Compressing folders..."                                                   
echo
echo
tar cvf $(date +%d-%m-%Y).tar /path/to/folders/you/want/to/backup;
echo
echo
echo "Done. Encrypting. Write a password...";
gpg -c $(date +%d-%m-%Y).tar;
echo "Moving to dropbox folder";
echo
mv $(date +%d-%m-%Y).tar.gpg /pathofyour/dropbox/folder;
echo "Removing tar file";
rm $(date +%d-%m-%Y).tar;
echo "tar file removed";
echo "Initializing dropbox daemon";
dropboxd &
sleep 180s;
echo "Closing dropbox daemon";
killall dropbox;
echo "Done!"

If you have built castles in the air, your work need not be lost; that is where they should be. Now put foundations under them.
Henry David Thoreau

Registered Linux User: #559057

Offline

#1919 2012-08-29 18:19:56

Mektub
Member
From: Lisbon /Portugal
Registered: 2008-01-02
Posts: 647

Re: Post your handy self made command line utilities

Shark,

nice.

Mektub


Follow me on twitter: https://twitter.com/johnbina

Offline

#1920 2012-08-29 19:21:41

Shark
Member
From: /dev/zero
Registered: 2011-02-28
Posts: 686

Re: Post your handy self made command line utilities

Mektub wrote:

Shark,

nice.

Mektub

Thanks. I made it out of realy banal reason. I use KDE and before KDE i used deja dup in XFCE. I like to have as little as possible gtk applications (nothing against them) in my system (yeah, i know. Totally stupid smile and that was the motive behind the script. I know there is qt based back-up applications out there but i just don't like them. I could use only duplicity but back then it was just to complicated for me smile

So, laziness and some weird GTK contamination fear can produce new knowledge and creativity smile


If you have built castles in the air, your work need not be lost; that is where they should be. Now put foundations under them.
Henry David Thoreau

Registered Linux User: #559057

Offline

#1921 2012-09-01 16:27:01

AaronBP
Member
Registered: 2012-08-06
Posts: 149
Website

Re: Post your handy self made command line utilities

Thanks for the people over here for helping me with these:

#!/bin/sh


for i in "$@"; do
	sudo -v
	cower -d "$i" &&
		cd "$i" &&
		makepkg -si --noconfirm
	cd "$OLDPWD"
	rm -rf "$i"
done
#!/bin/sh

cowinstall `cower -uqb --color=never`

Offline

#1922 2012-09-02 15:16:35

mhertz
Member
From: Denmark
Registered: 2010-06-19
Posts: 681

Re: Post your handy self made command line utilities

I use a free dropbox account to store all my dot-configs, /etc-configs and other backups, but I don't want to use the dropbox daemon or have any folders synched, and instead prefer to do it manually with two shell functions and the dropbox_uploader.sh script in my '~/.bin' dir:

drop() {
	dropbox_uploader.sh -u <username> -p <password> -f $1 -d /Public -v
}

dget() {
	curl -sO <url_to_dropbox_public_folder>/$1
}

Then when i've e.g. has made some changes to .vimrc, i run

drop .vimrc

and to get a new .vimrc(or overwrite the old):

dget .vimrc

Edit: Ohh, I see there's a syntax change in newer versions, as I haden't updated in a while...

Last edited by mhertz (2012-09-09 21:45:56)

Offline

#1923 2012-09-02 19:44:00

ObliviousGmn
Member
Registered: 2011-09-14
Posts: 49
Website

Re: Post your handy self made command line utilities

Shark wrote:

I use dropbox for storing some backups. This is a script i have made to fully automate it. I use gpg encryption of tar file too when uploading to dropbox.

You've actually inspired me to try this out for my own,
I always been bad at keeping backups of Configs I always mess around with. But now I can do it at a single command.

Preview the script

Steps:
Creates Folder and subFolders
Copies desired files and folders to the "Backup" dir
Creates Package and deletes "Backup" dir
Moves to Dropbox folder. smile

Keep in mind:
The Packages have the month and year on them. So if you update them within that same month the previous Package will be overwritten. I did this because I don't want too many back up folders.

#!/bin/bash    
#ObliviousGmn                                                                                 
#Automate backup for dropbox w/ pretty colors

USECOLOR="yes"
. /etc/rc.d/functions 

echo

stat_busy "Copying Configs"
mkdir Backups
mkdir Backups/Ncmpcpp
mkdir Backups/Weechat

cp .xinitrc Backups
cp .Xdefaults Backups
cp .bashrc Backups
cp .vimrc Backups

cp ~/.ncmpcpp/config Backups/Ncmpcpp/
cp ~/.weechat/weechat.conf Backups/Weechat/
cp ~/.weechat/irc.conf Backups/Weechat/

cp -ar ~/.config/luakit Backups
cp -ar ~/.config/openbox Backups
cp -ar ~/.config/ranger Backups
cp -ar ~/.config/tint2 Backups
cp -ar ~/Customize Backups
cp -ar ~/.themes/Gmnbox Backups
stat_done                                                                                                

stat_busy "Creating Backup Folder"
tar cf Backup_$(date +%m%Y).tar Backups;
stat_done

stat_busy "Sweeping up"
rm -rf Backups/
mv Backup_$(date +%m%Y).tar ~/.DropBox/Dropbox/;
stat_done

echo

Last edited by ObliviousGmn (2012-09-02 19:49:51)

Offline

#1924 2012-09-03 01:25:04

illusionist
Member
From: localhost
Registered: 2012-04-03
Posts: 498

Re: Post your handy self made command line utilities

# Script to optimize Archlinux mirrors by speed and sync status

1.   Makes a backup of your existing mirrorlist and updates it according to mirror status.
2.   Needs reflector.

#!/bin/bash
#
# Script to optimize Archlinux mirrors by speed and sync status
# Author: Illusionist
#

if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi

function progress {
  chars=( "-" "\\" "|" "/" )
  interval=0.2
  count=0

  while true
  do
    pos=$(($count % 4))
    echo -en "\b${chars[$pos]}"
    count=$(($count + 1))
    sleep $interval
  done
}


function stop_progress {
  exec 2>/dev/null
  kill $1
  echo -en "\bdone!\n"
}


function halt_progress {
  exec 2>/dev/null && cp /etc/pacman.d/mirrorlist.bak /etc/pacman.d/mirrorlist 
  kill $1
  echo -e "\nTerminated by user\nRestored mirrorlist from backup\nFor more information visit: https://wiki.archlinux.org/index.php/Mirror"
}

echo -e "Backing up existing mirrorlist..."
cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak

echo -n "retrieving latest mirrorlist...."
progress &
pid=$!

trap 'halt_progress $pid; exit' 2
/usr/bin/reflector -l 10 --sort rate --save /etc/pacman.d/mirrorlist.temp


echo -ne "\bdone!\n"
echo -en "sorting mirrors by speed...."


rankmirrors -n 5 /etc/pacman.d/mirrorlist.temp > /etc/pacman.d/mirrorlist

stop_progress $pid

echo -e "\nFor more information visit: https://wiki.archlinux.org/index.php/Mirror"

Last edited by illusionist (2012-12-03 20:41:34)


  Never argue with stupid people,They will drag you down to their level and then beat you with experience.--Mark Twain
@github

Offline

#1925 2012-09-05 06:21:17

AaronBP
Member
Registered: 2012-08-06
Posts: 149
Website

Re: Post your handy self made command line utilities

Get info from whatever package owns a file:

#!/bin/sh
package=$(pacman -Qqo "$1")
pacman -Qi $package

Offline

Board footer

Powered by FluxBB