You are not logged in.

#2101 2013-05-16 14:43:16

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

Updated script for downloading from tpb. Apparently they added another tracker and it resulted in issue with earlier script. Fixed now (Think so tongue)

#!/usr/bin/bash
#
# v-k 2013
#
###
            
errorout() { 
  echo "error: $*" >&2; 
  echo
  [[ -e "$tmp_dir" ]] && rm -r "$tmp_dir";
  exit 1;
}

message() { 
  echo "usage: ${0##*/} pattern"; exit 1; 
}

search_pb() {
  local str cntr
  str="$1"
  cntr=$2

  [[ -f "$tmp_dir/tpb.1.test.$cntr" ]] || curl -s "http://thepiratebay.sx/search/${str// /%20}/$cntr/7/0" > "$tmp_dir/tpb.1.test.$cntr"
  sed -n '/class="detName"/ {p;n;n;p;n;p;n;n;p;n;p;}' "$tmp_dir/tpb.1.test.$cntr" > "$tmp_dir/tpb.2.test"
  sed -n '/class="detName"/ s/.*>\(.*\)<\/a>/\1/p; n; s/^<a href="\(.*\)" title="Down.*/\1/p; n; s/.*Uploaded \(.*\)\&nbsp\;\(.*\), Size \(.*\)\&nbsp\;\(.*\),.*/\1 \2|\3\4/gp; n; s/[^0-9]//gp; n; s/[^0-9]//gp;' "$tmp_dir/tpb.2.test" > "$tmp_dir/tpb.result"
  sed -i '1,${N;N;N;N;s/\n/|/g}' "$tmp_dir/tpb.result"
}

display_res() {
  width=$(cut -d"|" -f1 "$tmp_dir/tpb.result" | wc -L)
  divider="$(printf "%-$(($width+53))s" " ")"
  i=1
  printf "\n $(tput bold)$(tput setaf 3)%2.2b | %-${width}.${width}s | %11.11b | %9.9b | %8.8b | %6.6b |$(tput sgr0) \n" "# " "NAME (Pg #$((pg_cntr+1)))" "DATE" "SIZE" "SEEDS(!)" "LEECHS"
  echo "${divider// /=}+"
  while read NAME LINK DATE SIZE SEED LEECH; do
    [[ ${#DATE} -ne 11 ]] && DATE="${DATE// /  }"
    printf " %2.2b | %-${width}.${width}s | %11.11b | %9.9b | $(tput setaf 2)%8.8b$(tput sgr0) | $(tput setaf 1)%6.6b$(tput sgr0) | \n" "$i" "$NAME" "$DATE" "$SIZE" "$SEED" "$LEECH"
    ((i++))
  done < "$tmp_dir/tpb.result"
  echo "${divider// /=}+"
}

download() {
  IFS=","
  for i in $sel; do
    link="$(head -n $i "$tmp_dir/tpb.result" | tail -n 1 | cut -d"|" -f2)"
    echo $link >> "$tmp_dir/links"
  done
  IFS="$IFS_BAK"
}

# main starts here
[[ -f /usr/bin/aria2c ]] || errorout "aria2c is needed for getting metadata from magnet links"

[[ $# -eq 1 ]] || message
tmp_dir="$(mktemp -d)"
cmd_arg="$1"
pg_cntr=0

while true; do
  clear

  search_pb "$cmd_arg" $pg_cntr

  IFS_BAK="$IFS"
  IFS="|"
  display_res
  IFS="$IFS_BAK"

  echo
  echo -n "Enter selection: "
  read sel

  [[ -z $sel ]] && errorout "Invalid/Empty choice !! exiting !!!"
  [[ $sel = n ]] && { ((pg_cntr++)); continue; }
  [[ $sel = b ]] && { ((pg_cntr--)); continue; }

  download

  echo 

  aria2c -q --bt-metadata-only --bt-save-metadata -i "$tmp_dir/links" &
  pid=$!

  while kill -0 $pid &> /dev/null; do
    printf "[.   ] Downloading torrents !!\r"
    sleep .4s
    printf "[ .  ] Downloading torrents !!\r"
    sleep .4s
    printf "[  . ] Downloading torrents !!\r"
    sleep .4s
    printf "[   .] Downloading torrents !!\r"
    sleep .4s
    printf "[  . ] Downloading torrents !!\r"
    sleep .4s
    printf "[ .  ] Downloading torrents !!\r"
    sleep .4s
  done

  printf "[DONE] Downloading torrents !!\n\n"
  break
done

rm -r "$tmp_dir" 

"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#2102 2013-05-16 15:21:49

portix
Member
Registered: 2009-01-13
Posts: 757

Re: Post your handy self made command line utilities

graph wrote:

Why is this better than tput? Because it is native where tput is a dependency?

Using raw ansi escape sequences is not better than using tput. tput queries the terminfo database and choses the correct escape sequence depending on the terminal, so tput is more portable than using raw sequences.

Offline

#2103 2013-05-20 17:43:06

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

Re: Post your handy self made command line utilities

Inspired by this post, I wrote a function for my bashrc which checks which processes (number and command line) have a variable in their environment:

vgrep() {
	for pro in $(grep -l $1 /proc/[0-9]*/environ 2>/dev/null); do
		dir=$(dirname $pro)
		printf "%4s %s\n" $(basename $dir) "$(cat $dir/cmdline 2>/dev/null)"
	done
}

or, to add which actual variables in the environment match the search term (and to add some colors):

vgrep() {
	for pro in $(grep -l $1 /proc/[0-9]*/environ 2>/dev/null); do
		dir=$(dirname $pro)
		printf "\033[38;5;78m[%0.4d]\033[38;5;111m %s\n" \
				$(basename $dir) "$(cat $dir/cmdline 2>/dev/null)"
		list="$(grep --binary-files=text -o "[A-Z0-9_]*$1[A-Z0-9_]*" \
				$pro 2>/dev/null)"
		echo -en "   \033[38;5;229m"
		for entry in $list; do
			echo -n " $entry"
		done
		echo -e "\033[0m"
	done
}

Last edited by Trilby (2013-05-21 16:36:22)


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

Offline

#2104 2013-05-23 00:58:15

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Post your handy self made command line utilities

OK, I've got a new one. It's REALLY simple, but helps me a lot. smile

I don't want my computer to install updates unless I know what's going to be updated (and I have time to deal with possible breakage), but I always want to at least download the updates. So, I changed my "update" alias to first download the packages and then ask me if I want to install them:

alias archupdate="sudo pacman -Syuw --noconfirm && sudo pacman -Syu"

Offline

#2105 2013-05-23 01:31:09

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

Re: Post your handy self made command line utilities

I just print the list of files to be updated:

alias updates="pacman -Qqu --dbpath /tmp/checkup-db-jason"

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2106 2013-05-23 01:48:52

dodo3773
Member
Registered: 2011-03-17
Posts: 818

Re: Post your handy self made command line utilities

drcouzelis wrote:

OK, I've got a new one. It's REALLY simple, but helps me a lot. smile

I don't want my computer to install updates unless I know what's going to be updated (and I have time to deal with possible breakage), but I always want to at least download the updates. So, I changed my "update" alias to first download the packages and then ask me if I want to install them:

alias archupdate="sudo pacman -Syuw --noconfirm && sudo pacman -Syu"

Oh awesome. Yeah I started doing the same thing recently (cause I start update and then not pay attention / go do something else). I just use -Su for the second part though (plus my aur stuff after):

alias update='sudo pacman -Syuw --noconfirm && sudo pacman -Su && yaourt -Su --aur'

Offline

#2107 2013-05-23 11:00:45

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

Re: Post your handy self made command line utilities

jasonwryan wrote:

I just print the list of files to be updated:

alias updates="pacman -Qqu --dbpath /tmp/checkup-db-jason"

Or use 'checkupdates'.

Offline

#2108 2013-05-23 11:27:13

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

Re: Post your handy self made command line utilities

karol wrote:
jasonwryan wrote:

I just print the list of files to be updated:

alias updates="pacman -Qqu --dbpath /tmp/checkup-db-jason"

Or use 'checkupdates'.

I usually do not revert to this sort of profanity, but... DAFUQ? Where does this come from?

2013-03-26	contrib: adding checkupdates	Kyle Keen

Okay. Now that's cool.

Offline

#2109 2013-05-23 11:44:32

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

Re: Post your handy self made command line utilities

Awebb wrote:

I usually do not revert to this sort of profanity, but... DAFUQ? Where does this come from?

It's been around for a while https://bbs.archlinux.org/viewtopic.php … 5#p1072495
Some issues got fixed and here we have it in pacman-contrib :-)

Offline

#2110 2013-05-23 18:09:05

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

Re: Post your handy self made command line utilities

karol wrote:
jasonwryan wrote:

I just print the list of files to be updated:

alias updates="pacman -Qqu --dbpath /tmp/checkup-db-jason"

Or use 'checkupdates'.

LOL. Thanks karol; it had never ocurred to me...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2111 2013-05-23 20:01:59

HalosGhost
Forum Moderator
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 2,095
Website

Re: Post your handy self made command line utilities

alias lsupdate="checkupdates && cower -u"

big_smile

All the best,

-HG

Offline

#2112 2013-05-23 23:16:44

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,281

Re: Post your handy self made command line utilities

karol wrote:
jasonwryan wrote:

I just print the list of files to be updated:

alias updates="pacman -Qqu --dbpath /tmp/checkup-db-jason"

Or use 'checkupdates'.

Which is why I love this thread - always find commands I didn't know existed smile


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus Prime B450 Plus, 32Gb Corsair DDR4, Cooler Master N300 chassis, 5 HD (1 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

#2113 2013-05-25 11:12:47

kriz
Member
Registered: 2009-06-29
Posts: 96

Re: Post your handy self made command line utilities

Inhibit KDE screensaver for xx minutes or hours

lightson.sh

while /bin/true; do qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity > /dev/null; sleep 59s; done

lon.sh

timeout $1 lightson.sh

example

# lon.sh 55m

inhibits screensaver for 55 minutes.

comes in handy with flash or vlc videos


„Je verdinglichter die Welt, je dichter das Netz, das der Natur überworfen wurde, desto mehr beansprucht ideologisch das Denken, das jenes Netz spinnt, seinerseits Natur, Urerfahrung zu sein." Theodor W. Adorno [aus: Wozu noch Philosopie]

Offline

#2114 2013-05-25 11:24:52

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

Re: Post your handy self made command line utilities

Wouldn't this be cleaner:

xset s off
sleep $1
xset s on

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

Offline

#2115 2013-05-25 14:32:19

kriz
Member
Registered: 2009-06-29
Posts: 96

Re: Post your handy self made command line utilities

Trilby wrote:

Wouldn't this be cleaner:

xset s off
sleep $1
xset s on

doesn'twork for me


„Je verdinglichter die Welt, je dichter das Netz, das der Natur überworfen wurde, desto mehr beansprucht ideologisch das Denken, das jenes Netz spinnt, seinerseits Natur, Urerfahrung zu sein." Theodor W. Adorno [aus: Wozu noch Philosopie]

Offline

#2116 2013-05-30 20:44:23

mid-kid
Member
Registered: 2013-05-07
Posts: 177

Re: Post your handy self made command line utilities

This one is very simple, but It's useful to me:

alias getchmod="stat --format %a $1"

If it ain't broke, you haven't tweaked it enough.

Offline

#2117 2013-05-30 20:52:56

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

Re: Post your handy self made command line utilities

Aliases don't take arguments ;P
You can also use

alias getchmod='stat --format "%n %a"'
$ getchmod foo test*
foo 644
test1 644
test2 644
test3 644

or use function to get a nicer formatting:

gmod () { stat --format "%n %a" $* | column -t; }
$ gmod foo test*
foo    644
test1  644
test2  644
test3  644

Offline

#2118 2013-05-31 20:02:37

teckk
Member
Registered: 2013-02-21
Posts: 524

Re: Post your handy self made command line utilities

Put a clock in the upper right corner of shell

while sleep 1
do tput sc
tput cup 0 $(($(tput cols)-29))
date
tput rc
done &

Offline

#2119 2013-06-01 07:48:01

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,281

Re: Post your handy self made command line utilities

teckk wrote:

Put a clock in the upper right corner of shell

while sleep 1
do tput sc
tput cup 0 $(($(tput cols)-29))
date
tput rc
done &

Love it smile - just added it to my custom bash startup


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus Prime B450 Plus, 32Gb Corsair DDR4, Cooler Master N300 chassis, 5 HD (1 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

#2120 2013-06-01 14:07:50

mid-kid
Member
Registered: 2013-05-07
Posts: 177

Re: Post your handy self made command line utilities

karol wrote:

Aliases don't take arguments ;P
-snip-

I'm not that great with bash, but as far as I know it works for me.

[mid-kid@schoolrocket ~]# alias getchmod="stat --format %a $1"
[mid-kid@schoolrocket ~]# getchmod
stat: missing operand
Try 'stat --help' for more information.
[mid-kid@schoolrocket ~]# getchmod Desktop/
755

If it ain't broke, you haven't tweaked it enough.

Offline

#2121 2013-06-01 14:32:13

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

Re: Post your handy self made command line utilities

Aliases simply ignore positional arguments. Yes, you still have to supply arguments, because the computer can't read your mind which file(s) you want to stat.
Try the examples I posted to see for yourself. Try changing

gmod () { stat --format "%n %a" $* | column -t; }

to an alias - it won't work because the arguments are expected to come in the middle.
In getchmod alias the arguments come at the end, so this creates the illusion that aliases can take arguments.

Edit:
In my example

alias getchmod='stat --format "%n %a"'
$ getchmod foo test*
foo 644
test1 644
test2 644
test3 644

'getchmod' alias gets expanded to 'stat --format "%n %a"' so the above command is identical to

$ stat --format "%n %a" foo test*

'stat' needs an argument, so running just

$ stat --format "%n %a"

does not work.

Last edited by karol (2013-06-01 14:39:17)

Offline

#2122 2013-06-01 15:24:37

ill
Member
From: US
Registered: 2011-06-22
Posts: 115

Re: Post your handy self made command line utilities

Just a simple alias for finding a running process as it's something I have to do frequently.

alias fp="ps aux|grep -v grep|grep -i $1"

Offline

#2123 2013-06-01 15:36:10

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

Re: Post your handy self made command line utilities

ill wrote:

Just a simple alias for finding a running process as it's something I have to do frequently.

alias fp="ps aux|grep -v grep|grep -i $1"

Try 'pgrep'.

Offline

#2124 2013-06-01 19:04:01

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

karol wrote:
ill wrote:

Just a simple alias for finding a running process as it's something I have to do frequently.

alias fp="ps aux|grep -v grep|grep -i $1"

Try 'pgrep'.

and instead of doing grep -v grep, try grep -i [f]irefox -- put first char in square brackets of whatever process you're trying to find. smile


"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#2125 2013-06-01 22:05:48

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Post your handy self made command line utilities

teckk wrote:

Put a clock in the upper right corner of shell

while sleep 1
do tput sc
tput cup 0 $(($(tput cols)-29))
date
tput rc
done &

If you use urxvt I think there's a perl extension that will provide that, too.
There are probably pros and cons of the two approaches - I used to use it but stopped so it must have had some irritating side effect that this approach may not have.
But thought it worth mentioning anyway. smile


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

Board footer

Powered by FluxBB