# Przydatne Skrypty na polskim forum

## fallow

Zgodnie z proponowanym pomyslem to watek w ktorym zamieszczane beda skrypty  ktore uznamy za przydatne.    Wszystkie ustalenia , dyskusja mieszcza sie w   TYM WATKU. Tu takze  nalezy zglaszac nowe skrypty do dodania.

 Jak zglosic i zamiescic skrypt ? 

Skrypty sa przechowywane w tym watku , zamiescic skypt w watku ktory sluzy do dyskusji nad skryptami ( link wyzej ) , jesli uznamy ze skrypt nadaje sie do zamieszczenia wowczas zostanie on dodany do watku ktory obecnie czytasz czyli "Przydatne Skrypty na polskim forum" , pozniej zostanie dodany do niego link w pierwszym poscie w tym watku. 

----------------LISTA----------------------------

 Skrypt # 1 

 Opis :  Pokazuje progress kompilacji pakietow via emerge

Autor : Gherald 

 Skrypt # 2 

 Opis :  pokazuje niepotrzebne pliki w /etc/ 

Autor : Nelchael 

 Skrypt # 3 

 Opis : skypt sluzacy do latwej , przyjemnej zmiany io schedulera dla danego urzadzenia 

Autor :  Genn  

 Skrypt # 4 

 Opis: Skrypt służy do automatycznego synchronizowania komputera(zalecane raz na tydzień - chodzi głównie o obciążenie serwerów rsync) 

Autor : Michael Panhorst ,  Oryginalny post  

 Skrypt # 5 

 Opis: Wyszukuje i wyświetla stare i potencjalnie niepotrzebne już pliki znajdujące się w distfiles 

Autor : Konstantin Shaposhnikov 

----------

## fallow

Opis : Pokazuje progress kompilacji pakietow via emerge 

Autor : Gherald

```

#!/bin/bash 

#eprogress v0.0.1, July 13, 2004 

#When given a package name this scipt is will track the progress of an ebuild by counting the number of .c and .cpp files in the portage temp directory and comparing it to the number of .o files that have been compiled so far. 

#When run without arguements it's useful for locating previous failed or interrupted ebuilds so they can be attempted again or deleted manually to free space. 

cd /var/tmp/portage 

printf "%40s%8s%8s%8s%9s" "PACKAGE" "TOTAL" "LEFT" "DONE" "PERCENT"; echo 

for i in $1*; do 

   A=`find $i/ -iname "*.c*" | wc -l` 

   if test $A -eq 0; then continue; fi 

   B=`find $i/ -iname "*.o*" | wc -l` 

   C=$[$A-$B] 

   D=`expr $B \* 100 / $A` 

   if test $D -gt 100; then D="??"; fi 

   printf "%40s%8s%8s%8s%7s" "$i" "$A" "$C" "$B" "$D"; echo 

done

```

 *poprawka by bartmasz  wrote:*   

> Proponuje zamienic przedostatnia linijke
> 
> ```
> printf "%40s%8s%8s%8s%7s" "$i" "$A" "$C" "$B" "$D"; echo
> ```
> ...

 

----------

## nelchael

Opis: pokazuje niepotrzebne pliki w /etc/

Autor: Nelchael

```
#!/bin/bash

BASH_COLOR_RESET="\e[0m"

BASH_COLOR_GREEN="\e[01;32m"

BASH_COLOR_RED="\e[01;31m"

FILES_OK=0

FILES_BAD=0

FILES_TOTAL=0

for i in `find /etc -type f | sort`; do

   echo -n "Checking \"${i}\"... "

   # Check

   jest="`grep -Hr \"${i}\" /var/db/pkg/* | head -n 1 | cut -d ':' -f 1 | sed -e 's,/CONTENTS,,g' -e 's,/var/db/pkg/,,g'`"

   FILES_TOTAL=$(($FILES_TOTAL + 1))

   if [ "x${jest}" = "x" ]; then

      echo -en "${BASH_COLOR_RED}NOT FOUND${BASH_COLOR_RESET}"

      FILES_BAD=$(($FILES_BAD + 1))

   else

      echo -en "${BASH_COLOR_GREEN}ok${BASH_COLOR_RESET} (${jest})"

      FILES_OK=$(($FILES_OK + 1))

   fi

   echo

done

echo "Files: ${FILES_TOTAL} Valid: ${FILES_OK} Unknown: ${FILES_BAD}"
```

----------

## fallow

skypt sluzacy do latwej , przyjemnej  :Smile:  zmiany io schedulera dla danego urzadzenia

Autor :  Genn 

```

#!/bin/bash

###***Authored by Genn***###

##E-Mail: TheWindRider@myrealbox.com

#Last Update: 1/18/05

#This script was created to change the currently used scheduler

#under the vivid-sources kernel.  

#(http://forums.gentoo.org/viewtopic.php?p=1337010, maintained by fallow and troll)

#There are probably other kernels that could use this script as

#well, but I'm not aware of them.

#This script must be executable.  If it does not work 

#'chmod 700 *this_script*' should do it.

#You can supply one optional arguement with this script.

#'script_name block-device' will run the script with the

#intend to interactively change the scheduler on the

#block device you specify.  The block device is just the 

#last portion of the device address without slashes.  

#IE, if you wanted to adjust the schedular on /dev/hda, 

#you would run 'this_script hda' and follow the on-screen 

#instructions.

#Now, I say the arguement is optional because you are 

#requred to set the DEFAULT_DRIVE variable to a valid block 

#device.  So, if you do not supply the arguement, the script 

#will use that block-device.  The variable is set by the 

#same rules that the arguement is supplied by; just use the 

#last portion of the address with no slashes.

#And for those of you wondering where the variable is,

DEFAULT_DRIVE=hda

#Now you've done everything you need to, go run the script,

#and ignore everything from here down.

##########################################################

#Verification of a valid default block device choice.

if [ -z `ls /sys/block | grep $DEFAULT_DRIVE` ]

  then

    echo "You chose a non-valid block device as default."

    echo "/dev/$DEFAULT_DRIVE does not exist."

    echo "Please change the variable DEFAULT_DRIVE in the script."

    exit

fi

#Verification of root level access

ROOT_UID=0

ERR_NOTROOT=67

if [ "$UID" != "$ROOT_UID" ]

then

  echo "You must be root to run this script."

  exit $ERR_NOTROOT

fi

#If an arguement is supplied this makes sure it is used.

#Or if none is supplied it uses the default choice.

if [ -n "$1" ]

then

  if [ -n `ls /sys/block | grep $1` ]

  then

    echo "Now using /dev/$1 for block device."

    DRIVE=$1

  else

    echo "You chose a non-valid block device."

    echo "/dev/$1 does not exist."

    exit 1

  fi

else 

  echo "No arguement supplied."

  echo "Defaulting to /dev/${DEFAULT_DRIVE} for block device."

  DRIVE=${DEFAULT_DRIVE}

fi

#Schedular file location

SCHEDLOC=/sys/block/${DRIVE}/queue/scheduler

#Verification of valid scheduler location

if [ `[ -f ${SCHEDLOC} ]` ! -a `[ -w ${SCHEDLOC} ]` ]

then

  echo "Uh oh, either $SCHEDLOC is not a file, or you do not "

  echo "have write access to it.  I would suggest you verify" 

  echo "that the location listed above is correct and that the "

  echo "value in the script did not get altered somewhow.  The "

  echo "standard location is: "

  echo "/sys/block/{some_block_device}/queue/scheduler.  If it "

  echo "is valid, try echoing something by hand to "

  echo "$SCHEDLOC.  If you're "

  echo "sure your using the right kernel, and none of that works, "

  echo "make a post in the vivid-sources thread.  On the other "

  echo "hand, if echoing by hand does work, send me an e-mail as "

  echo "you just discovered a bug."

  exit 1

fi

#Identification of different available schedulers.

SCHED1=`cat ${SCHEDLOC} | gawk '{print $1}'`

SCHED2=`cat ${SCHEDLOC} | gawk '{print $2}'`

SCHED3=`cat ${SCHEDLOC} | gawk '{print $3}'`

SCHED4=`cat ${SCHEDLOC} | gawk '{print $4}'`  

#User Interface portion

echo "List of available schedulers"

echo "The bracketed choice is the one currectly in use."

echo "Selecting this one does nothing."

echo "1 -  ${SCHED1}"

echo "2 -  ${SCHED2}"

echo "3 -  ${SCHED3}"

echo "4 -  ${SCHED4}"

echo " "

echo "Please type the number corresponding the scheduler you wish to use."

#This applies the selected change

read CHOICE

case ${CHOICE} in

1) 

echo "$SCHED1" > $SCHEDLOC

NEWSCHED=${SCHED1}

;;

2) 

echo "$SCHED2" > $SCHEDLOC

NEWSCHED=${SCHED2}

;;

3) 

echo "$SCHED3" > $SCHEDLOC

NEWSCHED=${SCHED3}

;;

4) 

echo "$SCHED4" > $SCHEDLOC

NEWSCHED=${SCHED4} 

;;

*) 

echo "You must input a numerical value corresponding to one of the not-in-use schedulers."

exit 1   

;;

esac

#Error catching if the current scheduler is chosen.

VALIDATE=`echo $NEWSCHED | grep -F '['`

if [ "${VALIDATE}" != "" ]

then

echo "You just chose the scheduler currently in use."

exit 1

fi

echo " "

#Announcement of what schedular was selected for which drive.

echo "Schedular changed to $NEWSCHED on /dev/${DRIVE}"

exit 0

```

----------

## fallow

Gentoo Linux Cron Update

Autor: Michael Panhorst

Oryginalny post: https://forums.gentoo.org/viewtopic.php?p=1304452#1304452

Wymagania: mailx, esearch, gentoolkit

Sposób instalacji: Najprostszy - umieszczenie skryptu w katalogu /etc/cron.weekly, inny - umieszczenie w crontabie.

Opis: Skrypt służy do automatycznego synchronizowania komputera(zalecane raz na tydzień - chodzi głównie o obciążenie serwerów rsync)

Co robi skrypt:

1. emerge sync

2. fixpackages, jeśli trzeba

3. Sprawdzenie GLSA

4. Jeśli wystąpiły błędy przy emerge sync albo są nowe GLSA, skrypt wysyła email z informacją

5. Sprawdzenie czy są nowe wersje (emerge system or emerge world)

6. Paczkowanie nowych wersji jeśli spełnione są zależności(emerge -B)

7. Wysłanie emaila (jeśli są pakiety do zaktualizowania), a także informacji w jaki sposób tego dokonać (używając juz skompilowanych paczek - emerge -avK)

```
#!/bin/bash 

# 

# 

# glcu - gentoo linux cron update 

# script for keeping your Gentoo linux up to date 

# 

# Version 0.65 - written by Michael Panhorst - Michael [at] untiefe [dot] de 

# 

# put this script in the /etc/cron.weekly directory 

# You need to have mailx, esearch and gentoolkit emerged 

# Set your eMail address first: 

EMAILADRESS=root 

# set if you want to test (and build) emerge 'system' or 'world' 

UPDATE=system 

################################################################ 

# You shouldn't need to change anything below this line 

PKGDIR="/usr/portage/packages" 

source /etc/make.conf  # in case you set PKGDIR different 

SUBJECT="glcu: New ebuilds for emerge $UPDATE on $HOSTNAME" 

FS="" 

EXTRA="" 

# 1. emerge sync ############## (sync first) #################### 

emerge sync 2>/tmp/emergesync.$$ 

# and run fixpackages (if needed) 

grep "Skipping packages. Run 'fixpackages' or set it in FEATURES" /tmp/emergesync.$$ 

if [ $? == 0 ] 

    then 

    echo "" >>/tmp/ecdep.$$ 

    echo "Running fixpackages" >>/tmp/ecdep.$$ 

    echo "" >>/tmp/ecdep.$$ 

    fixpackages 2>>/tmp/ecdep.$$ 

    echo "" >>/tmp/ecdep.$$ 

fi 

# 2. update database for esearch ################################ 

eupdatedb 2>>/tmp/emergesync.$$ 

# 3. glsa-check - check for security updates #################### 

glsa-check -l -n|grep " \[N\] " >/tmp/eupdate.$$ 

if [ $? == 0 ] 

    then 

    echo "" >>/tmp/emergesync.$$ 

    echo "----------------------------------------------------------------------" >>/tmp/emergesync.$$ 

    echo "Important Security updates for $HOSTNAME:" >>/tmp/emergesync.$$ 

    echo "" >>/tmp/emergesync.$$ 

    cat /tmp/eupdate.$$ >> /tmp/emergesync.$$ 

    echo "" >>/tmp/emergesync.$$ 

    echo "" >>/tmp/emergesync.$$ 

    echo "*** Run 'glsa-check --fix new' to auto-apply all security updates" >>/tmp/emergesync.$$ 

    echo "         ====================" >>/tmp/emergesync.$$ 

    echo "" >>/tmp/emergesync.$$ 

    echo "----------------------------------------------------------------------" >>/tmp/emergesync.$$ 

    FS="- SECURITY UPDATES!" 

fi 

# only send a mail if errors ocurred 

if [ -s /tmp/emergesync.$$ ] 

then 

    if [ -s /tmp/ecdep.$$ ] 

   then 

   cat /tmp/ecdep.$$ >> /tmp/emergesync.$$ 

    fi 

   cat /tmp/emergesync.$$ | mail -s "glcu($HOSTNAME): emerge sync $FS" $EMAILADRESS 

fi 

# 4. emerge -pv system/world (check for needed/new ebuilds) ### 

emerge --pretend --deep $UPDATE > /tmp/eupdate.$$ 2>&1 

# prebuild (emerge --buildpkgonly) all needed packages 

grep ebuild /tmp/eupdate.$$ 

if [ $? == 0 ] 

    then 

    echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" >> /tmp/ecdep.$$ 

    echo "List of new Packages:" > /tmp/ecdep.$$ 

    echo "---------------------" >> /tmp/ecdep.$$ 

    grep ebuild /tmp/eupdate.$$ >> /tmp/ecdep.$$ 

    echo "---------------------------------------------------------------------" >> /tmp/ecdep.$$ 

    for package in `cat /tmp/eupdate.$$ | grep ebuild | cut -f 2- -d "/" | cut -f 1 -d " "` ; 

      do 

      if [ ! -f $PKGDIR/All/$package.tbz2 ] 

     then 

     echo "" >> /tmp/ecdep.$$ 

     echo "Building binary package for $package :" >> /tmp/ecdep.$$ 

     emerge --pretend --buildpkgonly --oneshot =$package > /tmp/eupdate.$$ 

     if [ ` grep -c ebuild /tmp/eupdate.$$ ` == 1 ] 

         then 

         nice -n +19 emerge --buildpkgonly --oneshot =$package 2>> /tmp/ecdep.$$ 

         if [ $? == 0 ] 

        then 

        PACKAGES="$PACKAGES =$package" 

        else 

        echo "" >> /tmp/ecdep.$$ 

        echo " ***  Error while building $package!" >> /tmp/ecdep.$$ 

        SUBJECT="glcu: DEPENDENCY PROBLEM, COULDN'T PREBUILD ALL PACKAGES ON $HOSTNAME" 

        DEP=1 

         fi 

         else 

         echo " *** Dependencies for $package not met." >> /tmp/ecdep.$$ 

         echo " *** Cannot prebuilt $package!" >> /tmp/ecdep.$$ 

         SUBJECT="glcu: DEPENDENCY PROBLEM, COULDN'T PREBUILD ALL PACKAGES ON $HOSTNAME" 

         DEP=1 

     fi 

      fi 

    done 

    echo "New packages for 'emerge $UPDATE'!" > /tmp/glcu.$$ 

    echo "                  =============" >> /tmp/glcu.$$ 

    echo "" >> /tmp/glcu.$$ 

    if [ -n "$PACKAGES" ] 

   then 

   echo "To update your gentoo linux, execute:" >> /tmp/glcu.$$ 

   echo "" >> /tmp/glcu.$$ 

   echo "  emerge -avK $PACKAGES" >> /tmp/glcu.$$ 

   echo "" >> /tmp/glcu.$$ 

   EXTRA=further 

   SEND=1 

    fi 

    if [ $DEP ] 

   then 

   echo "check for $EXTRA packages with:" >> /tmp/glcu.$$ 

   echo "" >> /tmp/glcu.$$ 

   echo "  emerge -av $UPDATE" >> /tmp/glcu.$$ 

   echo "" >> /tmp/glcu.$$ 

   SEND=1 

    fi 

    cat /tmp/ecdep.$$ >> /tmp/glcu.$$ 

    # send mail, how to update the system 

    if [ $SEND ] 

   then 

   cat /tmp/glcu.$$ | mail -s "$SUBJECT" $EMAILADRESS 

    fi 

# rm all used files 

    rm /tmp/glcu.$$ 

fi 

rm /tmp/emergesync.$$ /tmp/eupdate.$$ /tmp/ecdep.$$
```

----------

## fallow

Find old distfiles

Autor: Konstantin Shaposhnikov

Opis: Wyszukuje i wyświetla stare i potencjalnie niepotrzebne już pliki znajdujące się w distfiles

```
#!/bin/bash

function create_portage_list() {

  find $1 -path '*files/digest-*' -type f | xargs -n1 awk '{print $3}' > $2

}

echo "Finding portage distfiles ... "

create_portage_list /usr/portage /tmp/portage-distfiles1

echo "Finding port overlay distfiles ... "

create_portage_list `portageq portdir_overlay` /tmp/portage-distfiles2

echo "Sorting list of distfiles ... "

cat /tmp/portage-distfiles{1,2} | sort > /tmp/portage-distfiles

rm /tmp/portage-distfiles{1,2}

echo "Finding local distfiles ... "

find /usr/portage/distfiles/ -type f | xargs -n1 basename | sort > /tmp/local-distfiles

comm -1 -3 /tmp/portage-distfiles /tmp/local-distfiles

```

----------

