Σελίδα 1 από 42

Τα πάντα για το Bash...

ΔημοσίευσηΔημοσιεύτηκε: 22 Μαρ 2009, 21:02
από ftso
Εδώ θα γράφουμε ότι αφορά το Bash και το (Bash) Shell Scripting...
Προβλήματα, ερωτήσεις, απαντήσεις, δικά μας shell scripts και ότι σχετικό.



Εικόνα


------------------------------------------------------------
~Παρακαλώ να γίνετε οπωσδήποτε χρήση του code tag όπου υπάρχει κώδικας~[/center]

Re: Τα πάντα για το Shell Scripting...

ΔημοσίευσηΔημοσιεύτηκε: 23 Μαρ 2009, 15:55
από Dimitris
Μη ξεχνάμε τον οδηγό για το τερματικό viewtopic.php?f=9&t=367 και την εισαγωγή στον προγραμματισμό κελύφους viewtopic.php?f=9&t=1545

Το αγαπημένο μου lazy scriptάκι :mrgreen: :
Κώδικας: Επιλογή όλων
#!bin/bash
make clean
make
./$1

Επειδή βαριέμαι να γράφω κάθε φορά make clean, make, τρέξε το εκτελέσιμο που προκύπτει. Σαν όρισμα δέχεται το όνομα του εκτελέσιμο που θα προκύψει από το make.

Re: Τα πάντα για το Shell Scripting...

ΔημοσίευσηΔημοσιεύτηκε: 23 Μαρ 2009, 16:11
από ftso
Αυτός είναι ο κώδικας του ProTV...
Το γραφικό δημιουργείτε με το zenity το οποίο είναι είναι αρκετά καλό και απλό για την εμφάνιση παραθυρικού...

Κώδικας: Επιλογή όλων
#!/bin/bash
#proTV is a bash script that shows the tv program of various Greek channels.

#Depends on:zenity

#Copyright (C) 2008 Dimitris Diamantis (aka ftso)
#This is free software. You may redistribute copies of it under the terms of
#the GNU General Public License <http://www.gnu.org/licenses/GPL.html>
#There is NO WARRANTY, to the extent permitted by law.

#in.gr webiste is copyright of Lambrakis Press

while [ 1 ];
do
system_encoding="utf8"
i_am=`whoami`

test1=/tmp/$i_am-mtv1
test2=/tmp/$i_am-mtv21
tes1=/tmp/$i_am-mtv2
tes2=/tmp/$i_am-mtv3
tes3=/tmp/$i_am-mtv4
tes4=/tmp/$i_am-mtv5

rm -f $test1 $tes1 $tes2 $tes3 $tes4 $test2
touch $test1 $tes1 $tes2 $tes3 $tes4 $test2

pdate=$(zenity --calendar --text "Διαλέξτε μέρα" --title "Date" --date-format=%Y-%m-%d);
if [ $? != 0 ]; then exit 0; fi

ping -c 1 www.google.gr
if [ $? != 0 ]; then
zenity --error --title="Error" --text="Connection Problem!"
exit 0;
fi

wget -O - "http://www.in.gr/TV/results.asp?dtmDate=$pdate" 2>&1 > $test2 | sed -u 's/.*\ \([0-9]\+%\)\ \+\([0-9.]\+\ [KMB\/s]\+\)$/\1\n# Downloading \2/' | zenity --width=300 --height=100 --progress --auto-close --title="Downloading Tv program from in.gr..." --text="Downloading Tv program from in.gr..."
if [ $? != 0 ]; then
rm -f $test1 $tes1 $tes2 $tes3 $tes4 $test2
exit 0;
fi
cat $test2 | iconv -f iso-8859-7 -t $system_encoding > $test1


zn=$(grep ChannelID $test1|grep -vE "class|size"|sort -u|sed -e 's/.*ChannelID=\(.*\)">\(.*\)<\/a.*/ \2/'|grep -E -n "[0-9A-Za-zα-ωΑ-Ω]"|awk '{print "FALSE " $1$2$3$4}')

input=$(zenity --title="Channels" --text="Επιλογή καναλιού" --list --radiolist --height=430 --width=295 --column "#" --column "Κανάλι" $zn --separator=" ")
if [ $? != 0 ]; then
rm -f $test1 $tes1 $tes2 $tes3 $tes4 $test2
exit 0;
fi

cn=$(echo $input | awk 'BEGIN { FS=":" } ; {print $1}')
cn1=$(echo $input | awk 'BEGIN { FS=":" } ; {print $2}')

Channel=`grep ChannelID $test1|grep -vE "class|size"|sort -u|sed -e 's/.*ChannelID=\(.*\)">\(.*\)<\/a.*/\1/'|sed -n ${cn}p`


grep -v class $test1|grep -C3 "ID=$Channel"|sed -e 's/<td>\(.*\)<\/td>/\1/'|sed -e 's/<a \(.*\)">\(.*\)<\/a>/\2/'|sed -n 1~8p|sed 's/[\t]*//' > $tes1
grep -v class $test1|grep -C3 "ID=$Channel"|sed -e 's/<td>\(.*\)<\/td>/\1/'|sed -e 's/<a \(.*\)">\(.*\)<\/a>/\2/'|sed -n 2~8p|sed 's/[\t]*//' > $tes2
grep -v class $test1|grep -C3 "ID=$Channel"|sed -e 's/<td>\(.*\)<\/td>/\1/'|sed -e 's/<a \(.*\)">\(.*\)<\/a>/\2/'|sed -n 6~8p|sed 's/[\t]*//' > $tes3

for ((i=1; i<=`cat $tes1 | wc -l`; i++));
do echo -e "`sed -n ${i}p $tes1`\t`sed -n ${i}p $tes2`\t\t\t\t\t\t\t`sed -n ${i}p $tes3`" >> $tes4 ; done
cat $tes4 | zenity --title="TV Program" --text="Το πρόγραμμα του καναλιού $cn1 για τις $pdate" --list --width 680 --height=380 --column="Ώρα" --column="Τίτλος" --column="Είδος" --separator=" "
if [ $? != 0 ]; then
rm -f $test1 $tes1 $tes2 $tes3 $tes4 $test2
exit 0;
fi
done



Λεπτομέρειες εδώ viewtopic.php?f=6&t=123&p=676#p676

shell flv to avi

ΔημοσίευσηΔημοσιεύτηκε: 18 Απρ 2009, 23:09
από sokoban4ever
Ένα sript που έκανα για τι είχα πολλά flv απο youtube και ήθελα να τα κάνω avi
ηταν ακόμα ποιό πρόχειρο αλλά το "εφτιαξα " λιγάκι
θέλει το espeak και το ffmpeg
Tο πετάς μέσα στο φάκελο με τα flvs το εκτελείς και στα κάνει avis η'mpg στο Desktop


Κώδικας: Επιλογή όλων
#!/bin/sh
# made by giannis in GPL >= 3 ;)
# Usage : drop me in a folder with flv videos and i make avis or mpg (with ffmpeg )


ffmpeg -h > /dev/null 2>/dev/null
if [ $? != 0 ]; then
echo "Install ffmpeg  --> sudo apt-get install ffmpeg"
return 1 
exit
fi;





giveformat(){
espeak -h > /dev/null 2>/dev/null
if [ $? != 0 ]; then
echo "missing espeak so try withou -s or instal it "
else

espeak -s 130 -v el "Δώσε το format  που θέλεις , μόνο άβι ή έμ μπί τζί "
fi

}



main(){
echo
echo "*******************************************************************************"
echo "*******************************************************************************"
echo "********extremely simple ffmpeg converter of many flvs"
echo "********Drop me in the directory with the flvs"
echo "********Working with file '$0' with  PID: $$"
echo "*******************************************************************************"
echo "*******************************************************************************"


echo

echo "Give the  format you want:  Type ONLY avi or mpg gege ?"
giveformat

read format


case $format in
    avi|mpg) ;;
    *)
    echo "******************************************************************"
    echo no i dont want to convert anything except avi or mpg  buxaxaxaxaxa 
    espeak -s 130 -v el "Υποστηρίζω μόνο άβι ή έμ μπέγκ  χα χα χα , λόλ "
    echo Try again 
    echo "******************************************************************"
   
    exit
    ;;
   
esac   

echo

}

convertstaff(){
for i in *.flv

do

echo "$0  is working in $i "
mkdir $HOME/Desktop/`date +"%d-%m-%y"`
if [ $? = 1 ]; then
echo
echo "****************************************************************"
echo "!!!!!!!File exists"
echo "!!!!!!!please delete $HOME/Desktop/`date +"%d-%m-%y"` and start again"
    espeak -s 160 -v el "Παρακαλώ διάγραψε το φάκελο στην επιφάνειας εργασίας η μετονόμασε το τώρα  "
    espeak -s 160 -v el " και μετα ξανά δοκίμασε να με εκτελέσεις"
echo "!!!!!!!bye bye"
echo "****************************************************************"
echo
kill $$
fi



c_name=`echo $i |sed s/.flv//`
echo trying to save at
echo $HOME/Desktop/`date +"%d-%m-%y"`/$c_name.$format
echo go..........

ffmpeg -i $i $HOME/Desktop/$c_name.$format $i

if [ $? != 0 ]
then
echo "****************************************************************"
echo "****************************************************************"
echo Something went wrong so i simply  kill mysef
echo  execute man ffmpeg and do it your self lol
espeak -s 160 -v el " Κάτι πήγε στραβά πρέπει να κοιτάξεις το μάν πέιτζ του έφ έφ έμ μπέγκ τώρα "
  espeak -s 140 -v en " do it your self i don't care,  figure it out with the, ffmpeg man page,  bye "
echo bye
echo "****************************************************************"
echo "****************************************************************"
kill $$
fi

echo "*****************DONE $c_name.$format***************************"

done

echo "--------------------------------------------------------------------"
echo "    pfff ... job finished "
echo "    $0 should have finished so PID $$ is no more used ;)          "
echo "    bye               "
echo "--------------------------------------------------------------------"
espeak -s 110 -v el "  ούφ . , η δουλειά έγινε, τα αρχεία πρέπει να μεταράπηκαν , πάπαλα "
echo

return 0
}


main
convertstaff

Re: Τα πάντα για το Shell Scripting...

ΔημοσίευσηΔημοσιεύτηκε: 19 Απρ 2009, 00:46
από Μάριος Ζηντίλης
Ελπίζω κάποια στιγμή να το δούμε ως επίσημη εφαρμογή στην πλατφόρμα GNOME, γιατί τρελαίνομαι να μεταφράσω αυτά τα σκαμπρόζικα μηνύματα του "γραφικού" περιβάλλοντος :lol: :lol: :lol:

viewline

ΔημοσίευσηΔημοσιεύτηκε: 19 Απρ 2009, 02:31
από medigeek
Κώδικας: Επιλογή όλων
#!/bin/bash
# Returns the line you want to see
# Usage: bash viewline myfile 4
head -n $2 "$1" | tail -n 1


$ bash viewline remoteftpbackup.pl 1
#!/usr/bin/perl

Cleanup script

ΔημοσίευσηΔημοσιεύτηκε: 19 Απρ 2009, 03:03
από medigeek
Καλά αυτό μπορεί να σας αρέσει περισσότερο :)
Χρειάζεται τα πακέτα: bash zenity gksu
και ένα desktop manager (gnome/xfce/kde?)

Σημείωση: ΔΕΝ φερω ευθύνη αν δημιουργήσει προβλήματα, ειδικά για το μέρος όπου κάνει εκκαθάριση του kernel, είναι δοκιμασμένο, αλλά και πάλι ποτέ δεν ξέρεις... :)
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Εικόνα

Κώδικας: Επιλογή όλων
#!/bin/bash
# Clean up script for the Ubuntu Operating System
# Requires: bash zenity gksu (and a desktop manager obviously)

#  Copyright (c) 2008 Savvas Radevic
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Clear variables
aptupdate=0
cspoweroff=0
aptnonpurged=""
aptkernels=""
grubkernels=""
kernelstopurge=""

# Main
IFS='|'
cleanuplist=`zenity --width 500 --height 300 --list --checklist --text "Choose what type of cleanup you wish:" --hide-column=2 --column "Do" --column "ID" --column "Description" \
TRUE 10 "APT: clears all unnecessary package dependencies (apt-get autoremove --purge)" \
TRUE 20 "APT: clears all unnecessary packages (apt-get autoclean)" \
FALSE 30 "APT: clears all cached/downloaded packages (apt-get clean)" \
TRUE 40 "APT: clears all configuration files of non-purged packages (aptitude purge non-purged packages)" \
FALSE 50 "Kernel: purge all obsolete/local GNU/Linux kernels (except the last two)" \
FALSE 100 "Temporary files: Clear Gedit/text editor temporary files in $HOME (removes *~ files)" \
FALSE 500 "Shutdown when done"`

for i in $cleanuplist; do
  if [ "$i" = "10" ]; then
    echo -e "\nAPT: Clearing unnecessary package dependencies.."
    gksu --description "Clean-up script" "apt-get autoremove --purge -y"
    aptupdate=1
  fi
  if [ "$i" = "20" ]; then
    echo -e "\nAPT: Removing unnecessary cached/downloaded packages.."
    gksu --description "Clean-up script" "apt-get autoclean"
    aptupdate=1
  fi
  if [ "$i" = "30" ]; then
    echo -e "\nAPT: Clearing all cached/downloaded packages.."
    gksu --description "Clean-up script" "apt-get clean"
    aptupdate=1
  fi
  if [ "$i" = "40" ]; then
    IFS=$'\n'
    echo -e "\nAPT: Clearing all non-purged packages.."
    aptnonpurged="`aptitude search '~c' -F '%p' | xargs -n 30`"
    if [ "$aptnonpurged" ]; then
      for line in $aptnonpurged; do
        gksu --description "Clean-up script" "aptitude -y purge $line"
      done
      aptupdate=1
    else
      echo "No non-purged packages found"
    fi
  fi
  if [ "$i" = "50" ]; then
    unset IFS
    echo -e "\nKernel: purge all obsolete/local GNU/Linux kernels (except the last two).."
    echo "Getting the last two kernels from grub.."
    grubkernels=`cat /boot/grub/menu.lst | grep '^title.*kernel' | grep -o 'kernel [0-9][^ ]*' | cut -d ' ' -f 2 | sort -u -r | head -n 2`
    echo "Getting the versions of the obsolete kernel packages.."
    aptkernels="`aptitude search '~o^linux-image' -F '%p' | cut -d- -f3-`"
    echo "Comparing kernel packages.."
    for k in $aptkernels; do
      kernelmatch=`echo "$grubkernels" | grep $k`
      if [ ! "$kernelmatch" ]; then
        if [ ! "$kernelstopurge" ]; then
          kernelstopurge+="$k"
        else
          kernelstopurge+=" $k"
        fi
      else
        echo "Ignoring $k (it's in the latest two kernels)"
      fi
    done
    if [ "$kernelstopurge" ]; then
      list="`for i in $kernelstopurge; do echo linux-image-$i; done`"
      echo "Will purge the following obsolete kernels: $list"
      gksu --description "Clean-up script" "aptitude -y purge $list"
    else
      echo "No actions taken"
    fi
  fi
  if [ "$i" = "100" ]; then
    echo -e "\nTemporary files: Clearing *~ temporary files in $HOME.."
    find $HOME -depth -name "*~" -delete
  fi
  if [ "$i" = "500" ]; then
    echo -e "\nShutdown requested.."
    cspoweroff=1
  fi
done
unset IFS

if [ "$aptupdate" = "1" ]; then
  echo -e "\nAPT: Updating/refreshing the APT package database.."
  gksu --description "Clean-up script" 'apt-get update'
fi

if [ "$cspoweroff" = "1" ]; then
  gksu --description "Clean-up script" gksu --description "Clean-up script (shutdown)" "shutdown -P now"
fi

Re: Τα πάντα για το Shell Scripting...

ΔημοσίευσηΔημοσιεύτηκε: 27 Απρ 2009, 01:28
από ftso
Zombie killer! Δεν είναι δικό μου, αλλά κάνει καλή δουλειά :D
Κώδικας: Επιλογή όλων
#!/bin/bash
# Zombie processes killing script.
# Must be run under root.
case "$1" in
--admin)
        stat=`ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print"pid: "$3" *** parent_pid: "$4" *** status: "$10" *** process: "$13}' | grep ": Z"`

        if ((${#stat} > 0));then
           echo zombie processes found:
       echo .
       ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print"pid: "$3" *** parent_pid: "$4" *** status: "$10" *** process: "$13}' | grep ": Z"
       echo -n "Kill zombies? [y/n]: "
       read keyb
       if [ $keyb == 'y' ];then
      echo killing zombies..
      ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print$4" status:"$10}' | grep "status:Z" | awk '{print $1}' | xargs -n 1 kill -9
       fi
   else
       echo no zombies found!
   fi
;;
--cron)
   stat=`ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print"pid: "$3" *** parent_pid: "$4" *** status: "$10" *** process: "$13}' | grep ": Z"`
        if ((${#stat} > 0));then
        ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print$4" status:"$10}' | grep "status:Z" | awk '{print $1}' | xargs -n 1 kill -9
   echo `date`": killed some zombie proceses!" >> /var/log/zombies.log
   fi
;;
*)   echo 'usage: zombies {--cron|--admin}'
;;
esac
exit 0

Re: Τα πάντα για το Shell Scripting...

ΔημοσίευσηΔημοσιεύτηκε: 27 Απρ 2009, 01:45
από sokoban4ever
Off topic:
cool :P και δεν τα πάω καθόλου τα zombies :mrgreen:

Re: Τα πάντα για το Shell Scripting...

ΔημοσίευσηΔημοσιεύτηκε: 27 Απρ 2009, 01:52
από ftso
sokoban4ever έγραψε:Off topic:
cool :P και δεν τα πάω καθόλου τα zombies :mrgreen:

Αν θες και γραφικό (για να βλογήσω και τα γένια μου) http://www.kde-apps.org/content/show.ph ... tent=77291 :mrgreen: