Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Useful command line scripts
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
defer-
Tux's lil' helper
Tux's lil' helper


Joined: 11 Jun 2007
Posts: 138
Location: Finland

PostPosted: Sat Jul 20, 2013 11:33 am    Post subject: Useful command line scripts Reply with quote

Post here your self made useful command line scripts. Any feedback or improvements would also be nice.

Heres what i have done:

CLI Wlan Manager Uses iw, iproute2, wpa_supplicant and dhcpcd.
EDIT: Deprecated in favor of C CLI Wlan Manager. You can find newest version from my github.

Logs
Code:
logs() {
   echo "DMESG :"
   dmesg | tail -n10

   echo -e "\nSYSLOG :"
   tail -n10 /var/log/messages

   echo -e "\nNETSTAT :"
   /sbin/ss -tunp
}

_________________
https://github.com/defer-


Last edited by defer- on Wed Aug 07, 2013 10:49 am; edited 7 times in total
Back to top
View user's profile Send private message
megabaks
Apprentice
Apprentice


Joined: 22 Jan 2012
Posts: 253
Location: Russia && Saint-Petersburg

PostPosted: Sat Jul 20, 2013 2:39 pm    Post subject: Reply with quote

hmmm...
why-masked:
Code:
#!/bin/bash

FILE="/usr/portage/profiles/package.mask"
opt="$(sed 's|/|\\/|' <<< "${1}")"
grep -20 "^[^#].*${opt}" "${FILE}" | sed -ne "/^#.*${opt}/d" -e '/./{H;$!d;}' -ne "x;/${opt}/p;" | sed '/^$/d'
Code:
[ megabaks@desktop ] ~ $ why-masked chromium
# Mike Gilbert <floppym@gentoo.org> (18 Jul 2013)
# Dev channel releases are only for people who are developers or want more
# experimental features and accept a more unstable release.
>=www-client/chromium-30
>=dev-lang/v8-3.20
[ megabaks@desktop ] ~ $

bugzilla:
Code:
#!/bin/bash

BGO="https://bugs.gentoo.org"

green="\033[01;32m"
red="\033[01;31m"
restore="\033[00m"
contrast="\033[1m"
underline="\033[4m"

if ! grep -q "bug=" <<< "${1}";then
   URL="https://bugs.gentoo.org/buglist.cgi?quicksearch=${1}"
   CONTENT="$(curl -s ${URL} 2>/dev/null)"
   BUGS="$(echo "${CONTENT}" | grep "a href=\"show_bug.cgi?id=.*[a-z].*\<" | cut -d \= -f2,3 | sed -e 's|>.*||' -e 's|\"||g')"
   for bug_full in ${BUGS};do
      url="${BGO}/${bug_full}"
      id="${bug_full#*=}"
      desc="$(grep "${bug_full}.*[a-z].*\<" <<< "${CONTENT}")"
      desc="${desc#*\>}"
      desc="${desc%%\<*}"
      status="$(echo "${CONTENT}" | grep -A1 ${id} | awk '/bz_[[:upper:]]/ {print $3}' | sed 's|bz_||')"
      echo -e "${status}\t${green}${id}${restore}: ${desc}" | sed -e 's|&quot;|\"|g;s|&gt;|\>|g;s|&lt;|\<|g'
   done
else
   id="${1#bug=}"
   URL="https://bugs.gentoo.org/show_bug.cgi?id=${id}"
   CONTENT="$(curl -s ${URL} 2>/dev/null)"
   status="$(echo "${CONTENT}" | awk '/bz_status/ {print $3}' | sed 's|bz_status_||')"
   desc="$(grep "Bug ${id} &ndash" <<< "${CONTENT}")"
   desc="${desc#*\;}"
   desc="${desc%\<*}"
   echo -e "${status}\t${green}${id}${restore}: ${desc}" | sed -e 's|&quot;|\"|g;s|&gt;|\>|g;s|&lt;|\<|g'
   echo -e "Attachments:"
   while read line;do
      name="${line#*title=\"}"
      name="${name%\"*}"
      href="${BGO}/attachment.cgi?id=${line%&amp*}"
      [[ -n "${name}" ]] && echo -e "${underline}${href}${restore} ${name}"
   done <<< "$(echo "${CONTENT}" | grep "href=\"attachment.cgi?id=.*title" | sed 's|.*id=||')"
fi
Code:
[ megabaks@desktop ] ~ $ bgo deadbeef
CONFIRMED   301349: new ebuild: media-sound/deadbeef       
[ megabaks@desktop ] ~ $ bgo bug=301349
CONFIRMED   301349:  new ebuild: media-sound/deadbeef
Attachments:
https://bugs.gentoo.org/attachment.cgi?id=216783 media-sound/deadbeef-0.3.2.ebuild
https://bugs.gentoo.org/attachment.cgi?id=216785 NSF MIME definition
https://bugs.gentoo.org/attachment.cgi?id=216787 SPC MIME definition
https://bugs.gentoo.org/attachment.cgi?id=222259 media-sound/deadbeef-0.3.3
https://bugs.gentoo.org/attachment.cgi?id=225329 media-sound/deadbeef-0.3.3.ebuild
https://bugs.gentoo.org/attachment.cgi?id=225379 Corrected ebuild with libnotify support
https://bugs.gentoo.org/attachment.cgi?id=228713 media-sound/deadbeef-0.4_beta1.ebuild
https://bugs.gentoo.org/attachment.cgi?id=230929 media-sound/deadbeef-0.4.0.ebuild
https://bugs.gentoo.org/attachment.cgi?id=234315 deadbeef-0.4.1.ebuild
https://bugs.gentoo.org/attachment.cgi?id=251005 working ebuild from funtoo's portage.
https://bugs.gentoo.org/attachment.cgi?id=252455 deadbeef-0.4.3_rc1-r1.ebuild
https://bugs.gentoo.org/attachment.cgi?id=254481 bump
https://bugs.gentoo.org/attachment.cgi?id=256715 media-sound/deadbeef-0.4.4-r1.ebuild
https://bugs.gentoo.org/attachment.cgi?id=256717 shorten license
https://bugs.gentoo.org/attachment.cgi?id=257087 media-sound/deadbeef-9999.ebuild
https://bugs.gentoo.org/attachment.cgi?id=257815 media-sound/deadbeef-9999.ebuild
https://bugs.gentoo.org/attachment.cgi?id=273303 deadbeef-0.5.0_rc2.ebuild
https://bugs.gentoo.org/attachment.cgi?id=273463 deadbeef git ebuild tag version
https://bugs.gentoo.org/attachment.cgi?id=273481 ebuild with ffap flag
https://bugs.gentoo.org/attachment.cgi?id=273483 metadata with ffap flag
https://bugs.gentoo.org/attachment.cgi?id=274317 deadbeef-0.5.1.ebuild
https://bugs.gentoo.org/attachment.cgi?id=274355 build.log: emerge failed on compile state without intltool
https://bugs.gentoo.org/attachment.cgi?id=274361 deadbeef-0.5.1.ebuild
https://bugs.gentoo.org/attachment.cgi?id=274465 deadbeef-0.5.1-r1.ebuild doc installation fixed
https://bugs.gentoo.org/attachment.cgi?id=274497 deadbeef-0.5.1-r1.ebuild, small fixes. latest.
https://bugs.gentoo.org/attachment.cgi?id=286649 deadbeef-0.5.1-r3.ebuild
https://bugs.gentoo.org/attachment.cgi?id=295791 ebuild and patch with virtual/ffmpeg depend
[ megabaks@desktop ] ~ $

zugaina:
Code:
#!/bin/bash
####################################
####### colors #####################
green="\033[01;32m"
red="\033[01;31m"
restore="\033[00m"
contrast="\033[1m"
####################################
echo
URL="http://gpo.zugaina.org"
####################################
####### search #####################
search(){
   for page in ${PAGES};do
      OUT="$(curl -s ${URL}/${page})"
      OVERLAYS="$(grep -o "Overlays/.*\"" <<< "${OUT}" | sed 's|Overlays/||;s|"||' | sort -u)"
      VERSIONS="$(grep -o "<b>.*</b" <<< "${OUT}" | sort -ru | sed -e 's|<b>||;s|</b||' -e "s|${PN}-||")"
      for ver in ${VERSIONS};do
         overlays="$(sed -n "/<b>${PN}-${ver}<\/b>/,/<\/li>/p" <<< "${OUT}" | grep -o "Overlays/.*\"" | sed 's|Overlays/||;s|"||' | sort -u)"
         FULL+="${CATEGORY}/${PN}-${ver}:"${overlays//$'\n'/ }$'\n'
      done
   done
}
####################################
####### go #########################
if [[ "${1}" == "-e" ]] && grep -q \/ <<< "${2}";then
   PAGES="${2}"
   CATEGORY="$(awk -F '/' '{print $1}' <<< "${2}")"
   PN="$(awk -F '/' '{print $2}' <<< "${2}")"
   desc="$(curl -s ${URL}/${2} | grep -o "<h5.*/h5>")"
   desc="${desc#*>}"
   desc="${desc%<*}"
   echo -e "${green}${2}${restore}: ${desc}"
   search
else
   PAGE="$(curl -s ${URL}/Search?search="${1}")"
   while read package desc;do
      [[ -z "${package}" ]] && continue
      echo -e "${green}${package}${restore}: ${desc}"
      package="$(qatom ${package})"
      CATEGORY="$(awk '{print $1}' <<< "${package}")"
      PN="$(awk '{print $2}' <<< "${package}")"
      SUB_PAGE="$(curl -s http://gpo.zugaina.org/${CATEGORY}/${PN})"
      PAGES="$(grep -o "<a href=\".*#.*\">" <<< "${SUB_PAGE}" | awk -F '/' '{print $2,$3}' | tr ' ' '/' | sort -u)"
      search;
   done <<< "$(sed -n '/<div id="search_results">/,/<div class="pager">/p' <<< "${PAGE}" | grep '<div>.*</div>' | sed 's|<div>||g;s|<\/div>||g')"
fi
####################################
####### output #####################
[[ -n "${FULL}" ]] && echo && echo -e "PACKAGE:REPOSITORY\n-------:----------\n${FULL}" | column -t -s ':'
Code:
[ megabaks@desktop ] ~ $ zugaina deadbeef

media-sound/deadbeef: foobar2000-like music player.
media-plugins/deadbeef-vk: DeadBeef plugin for listening musing from vkontakte.com
media-plugins/deadbeef-qt: Qt interface plugin for Deadbeef player
media-plugins/deadbeef-infobar: Infobar plugin for DeadBeeF audio player. Shows lyrics and artist's biography for the current track.
media-plugins/deadbeef-fb: DeaDBeeF filebrowser plugin
media-plugins/deadbeef-gnome: Adds support for Gnome multimedia keys to DeaDBeeF player. Should work only in Gnome and Unity DEs.
media-plugins/deadbeef-remote: Remote control plugin for DeadBeeF
media-plugins/deadbeef-ffmpeg: Complete solution to record, convert and stream audio and video. Includes libavcodec.
media-plugins/deadbeef-mpris-plugin: MPRIS-plugin for deadbeef
media-plugins/deadbeef-plugins-infobar: A plugin that allows you to view lyrics, biography, list of similar artists
media-plugins/deadbeef-archreader: Dedbeef plugin for play files directly from RAR, 7z and Gzip archive files
media-plugins/deadbeef-plugins-bs2b: Headphone crossfeed plugin using libbs2b
media-plugins/deadbeef-plugins-jack: Headphone crossfeed plugin using libbs2b

PACKAGE                                     REPOSITORY
-------                                     ----------
media-sound/deadbeef-9999-r4                stuff
media-sound/deadbeef-9999                   bar-overlay damex-overlay gentoo-zh
media-sound/deadbeef-0.5.6-r3               stuff
media-sound/deadbeef-0.5.6                  bar-overlay calculate miramir sabayon
media-sound/deadbeef-0.5.5                  foo-overlay funtoo miramir sabayon
media-sound/deadbeef-0.5.2                  foo-overlay funtoo
media-sound/deadbeef-0.5.1-r3               gentoo-zh
media-sound/deadbeef-0.5.1-r1               foo-overlay funtoo
media-sound/deadbeef-0.4.4                  sebasmagri
media-sound/deadbeef-0.4.3_rc1-r1           betagarden
media-plugins/deadbeef-vk-9999              stuff
media-plugins/deadbeef-vk-0.1.12            stuff
media-plugins/deadbeef-vk-0.1.11            stuff
media-plugins/deadbeef-qt-9999              stuff
media-plugins/deadbeef-infobar-9999         stuff
media-plugins/deadbeef-infobar-1.3          stuff
media-plugins/deadbeef-fb-20121012          stuff
media-plugins/deadbeef-fb-20120827          stuff
media-plugins/deadbeef-gnome-9999           stuff
media-plugins/deadbeef-remote-9999          stuff
media-plugins/deadbeef-ffmpeg-0.10.6        stuff
media-plugins/deadbeef-mpris-plugin-9999    stuff
media-plugins/deadbeef-mpris-plugin-2.1.2   stuff
media-plugins/deadbeef-plugins-infobar-1.3  bar-overlay
media-plugins/deadbeef-archreader-9999      stuff
media-plugins/deadbeef-plugins-bs2b-9999    bar-overlay
media-plugins/deadbeef-plugins-jack-9999    bar-overlay
[ megabaks@desktop ] ~ $ zugaina -e media-sound/deadbeef

media-sound/deadbeef: foobar2k-like music player

PACKAGE                            REPOSITORY
-------                            ----------
media-sound/deadbeef-9999-r4       stuff
media-sound/deadbeef-9999          bar-overlay damex-overlay gentoo-zh
media-sound/deadbeef-0.5.6-r3      stuff
media-sound/deadbeef-0.5.6         bar-overlay calculate miramir sabayon
media-sound/deadbeef-0.5.5         foo-overlay funtoo miramir sabayon
media-sound/deadbeef-0.5.2         foo-overlay funtoo
media-sound/deadbeef-0.5.1-r3      gentoo-zh
media-sound/deadbeef-0.5.1-r1      foo-overlay funtoo
media-sound/deadbeef-0.4.4         sebasmagri
media-sound/deadbeef-0.4.3_rc1-r1  betagarden
[ megabaks@desktop ] ~ $
and more ---> https://github.com/megabaks
_________________
sorry my bad english
Back to top
View user's profile Send private message
666threesixes666
Veteran
Veteran


Joined: 31 May 2011
Posts: 1237
Location: 42.68n 85.41w

PostPosted: Sat Jul 20, 2013 3:52 pm    Post subject: Reply with quote

/usr/sbin/update-grub2
Code:

#!/bin/bash
#make sure boot is mounted to load the kernel
mount | grep "/boot" /proc/self/mounts > /dev/null
if [ $? -ne 0 ]; then
mount /boot
fi
#make sure you have a grub2 directory in boot
if [ ! -d "/boot/grub2" ]; then
   mkdir /boot/grub2
fi
#dynamically assign the drive to install grub2 to
DRIVE=$(cat /proc/self/mounts | grep boot | awk '{print $1}' | sed 's/[0-9]*//g')
echo "installing grub2 to $DRIVE"
/usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg
/usr/sbin/grub2-install $DRIVE

_________________
cat /etc/*-release
Funtoo Linux - baselayout 2.2.0
consider this warning no. 1
https://wiki.gentoo.org/index.php?title=Special:Contributions/666threesixes666&offset=&limit=500&target=666threesixes666


Last edited by 666threesixes666 on Fri Aug 02, 2013 2:49 pm; edited 1 time in total
Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1155

PostPosted: Sat Jul 20, 2013 3:56 pm    Post subject: genuse Reply with quote

Shows meaning of USE

genuse <USE-flag-name>
Code:
#!/bin/bash
(
fgrep ":$1" /usr/portage/profiles/use.local.desc
echo  "---"
egrep "^$1" /usr/portage/profiles/use.desc
) | less -S

_________________
fun2gen2
Back to top
View user's profile Send private message
defer-
Tux's lil' helper
Tux's lil' helper


Joined: 11 Jun 2007
Posts: 138
Location: Finland

PostPosted: Fri Aug 02, 2013 11:55 am    Post subject: Reply with quote

colors.sh - Print table of terminal colors with escape sequences
Code:
#!/bin/env bash

t='abcd'

declare -a fg
declare -a bg
declare -a fg2

for i in {0..7}; do
   fg[$i]="\e[3${i}m"
   fg2[$i]="\e[1;3${i}m"
   bg[$i]="\e[4${i}m"
done

echo -en "       "
echo ${bg[@]}

for f in ${fg[@]} ${fg2[@]}; do
   echo -en "$f $t "
   for b in ${bg[@]}; do
      echo -en "$f $b $t \e[00m"
   done
   echo " $f"
done


extract.sh - Extract almost any archive. Thanks to Emmanuel Rouat for the idea.
Code:
#!/bin/env bash

if [ -z "$1" ]; then
   echo "Usage: extract [archive]"

if [ -f "$1" ]; then
   case "$1" in
      *.tar.bz2)  tar xjf "$1"      ;;
      *.tbz2)     tar xjf "$1"      ;;
      *.tar.gz)   tar xzf "$1"      ;;
      *.tgz)      tar xzf "$1"      ;;
      *.tar.xz)   tar xJf "$1"      ;;
      *.tar.Z)    tar xzf "$1"      ;;
      *.bz2)      bunzip2 "$1"      ;;
      *.gz)       gunzip "$1"       ;;
      *.xz)       unxz "$1"         ;;
      *.Z)        uncompress "$1"   ;;
      *.tar)      tar xf "$1"       ;;
      *.deb)      ar x "$1"         ;;
      *.7z)       7z x "$1"         ;;
      *.rar)      unrar x "$1"      ;;
      *.zip)      unzip "$1"        ;;
      *.jar)      unzip "$1"        ;;
      *.exe)      cabextract "$1"   ;;
      *)          echo "'$1' cannot be extracted" ;;
   esac
else
   echo "'$1' is not a file"
fi

_________________
https://github.com/defer-
Back to top
View user's profile Send private message
tomk
Administrator
Administrator


Joined: 23 Sep 2003
Posts: 7219
Location: Sat in front of my computer

PostPosted: Fri Aug 02, 2013 6:20 pm    Post subject: Reply with quote

Moved from Portage & Programming to Documentation, Tips & Tricks as it fits better here.
_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
Naib
Advocate
Advocate


Joined: 21 May 2004
Posts: 4119
Location: Removed by Neddy

PostPosted: Fri Aug 02, 2013 6:53 pm    Post subject: Reply with quote

deadlib() { lsof | grep 'DEL.*lib' | cut -f 1 -d ' ' | sort -u; }


nice bash fn I run after an emerge on my headless to see what programs/services need to be relaunched to use the updated binaries
_________________
A free press is the unsleeping guardian of every other right that free men prize; it is the most dangerous foe of tyranny. Where men have the habit of liberty, the Press will continue to be the vigilant guardian of the rights of the ordinary citizen.
Back to top
View user's profile Send private message
defer-
Tux's lil' helper
Tux's lil' helper


Joined: 11 Jun 2007
Posts: 138
Location: Finland

PostPosted: Thu Aug 15, 2013 4:03 pm    Post subject: Reply with quote

repeat.sh - Repeat command $x times after $y delay.
Code:
if [[ "$1" =~ ^[0-9]+$ ]] && [[ "$2" =~ ^[0-9]+$ ]]; then   
   max=$1
   count=$2
   shift
   shift
   for ((i=1; i <= max ; i++)); do
      sleep $count
      eval "$@"
   done
else
   echo "Usage: $0 [count] [interval] command"
fi

_________________
https://github.com/defer-
Back to top
View user's profile Send private message
The Doctor
Veteran
Veteran


Joined: 27 Jul 2010
Posts: 1470

PostPosted: Thu Aug 15, 2013 8:25 pm    Post subject: Reply with quote

i3_halt to cleanly exit X and shutdown without the added bloat of using a login manager. Could be easily adapted to another wm.
Code:
#!/bin/bash
nohup bash -c 'i3-msg exit; sudo halt' > /dev/null 2>&1



initramfs.sh - collects all the binaries necessary to make a basic initramfs finishes the job. It does require an init file in /usr/src/initramfs
Code:
#!/bin/bash
# script to regeorate the initramfs
# requires mkdir /usr/src/initramfs/{bin,dev,etc,lib,mnt,proc,root,sbin,sys}

# initial setup move the the right directories and get ready
cd /usr/src/initramfs
cp -a /dev/{null,console,tty,sda1} /usr/src/initramfs/dev/

# copy the firmware over
# not stricly required since its built into the kernel.
cp -a /lib/firmware/radeon/R700_rlc.bin /usr/src/initramfs/lib/firmware/radeon/R700_rlc.bin

cp -a /bin/busybox /usr/src/initramfs/bin/busybox

# crypt binaries
cp -a /sbin/cryptsetup /usr/src/initramfs/sbin/
cp -a /dev/{random,urandom} /usr/src/initramfs/dev/

# lvm binary
#cp -a /sbin/lvm /usr/src/initramfs/sbin/lvm
cp -a /sbin/lvm.static /usr/src/initramfs/sbin/lvm

ln -sf /usr/src/initramfs/sbin/lvm /usr/src/initramfs/sbin/lvchange
ln -sf /usr/src/initramfs/sbin/lvm /usr/src/initramfs/sbin/vgscan

find . -print0 | cpio --null -ov --format=newc | gzip -9 > /boot/hardened-initramfs.cpio.gz

_________________
First things first, but not necessarily in that order.
Back to top
View user's profile Send private message
TomWij
Developer
Developer


Joined: 04 Jul 2012
Posts: 1551

PostPosted: Fri Aug 16, 2013 8:37 am    Post subject: Reply with quote

megabaks wrote:
hmmm...
why-masked:
Code:
#!/bin/bash

FILE="/usr/portage/profiles/package.mask"
opt="$(sed 's|/|\\/|' <<< "${1}")"
grep -20 "^[^#].*${opt}" "${FILE}" | sed -ne "/^#.*${opt}/d" -e '/./{H;$!d;}' -ne "x;/${opt}/p;" | sed '/^$/d'
Code:
[ megabaks@desktop ] ~ $ why-masked chromium
# Mike Gilbert <floppym@gentoo.org> (18 Jul 2013)
# Dev channel releases are only for people who are developers or want more
# experimental features and accept a more unstable release.
>=www-client/chromium-30
>=dev-lang/v8-3.20
[ megabaks@desktop ] ~ $


Wrote something like this too a while ago, after finding how to awk a paragraph I made a one liner for all files; enjoy:

Quote:
find /usr/portage/profiles/ -name 'package.mask' -exec awk -vRS= '/www-client\/chromium/ { print " " FILENAME ":", "\n" "\n" $0 "\n" }' {} + | less
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum