Personal tools

Scripturi BASH

From linux360

Revision as of 16:54, 1 June 2006 by Raptor (talk | contribs) (Exemple scripturi BASH)
Jump to: navigation, search

Exemple scripturi BASH

Script care lanseaza in fundal o anumita comanda data ca parametru de intrare

Puteti lasa o comanda sa se execute in fundal fara ca ea sa fie intrerupta la iesirea dumneavoastra din sistem folosind urmatorul script pe care l-am numit silent. (output-ul nu este salvat, pentru aceasta puteti folosi nohup)

<sh/>#!/bin/bash

echo "Executing \"$*\"" setsid bash -c $* 2>>/dev/null 1>>/dev/null < /dev/null &

Scriptul il puteti folosi de exemplu:

$ silent wget -c -t 0 www.bigfiles.com/ubuntu.iso

Script ce elimina zona asociata unui domeniu dat ca parametru de intrare

Puteţi elimina porţiunea din named.conf de la zone "nume.domeniu.dat.ca.parametru.de.intrare" { până la acolada închisă asociată acoladei deschise de mai sus.

Scriptul este:<sh/>#!/bin/bash

  1. Bind zone remove script
  2. Copyright (C) 2005 Silvian Cretu <silvian86@yahoo.com>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

if [ ! $# = 1 ]; then

   echo "Usage: $0 domain";
   echo "The path to named.conf is defined inside the script";
   		 else

pathToNamedDotConf=/etc/named.conf

x=$(grep -n "zone \"$1\" {" $pathToNamedDotConf | cut -f1 -d:) sed $x,/\}\;/d $pathToNamedDotConf > temp c=$(sed -n $x'p' < temp) if [ "x`echo $c | grep "zone"`" = "x" ] then sed $x' d' temp > $pathToNamedDotConf else cat temp > $pathToNamedDotConf fi rm -f temp exit fi

Cautarea unui fisier ce contine un anumit string

<sh/>find /path -name "*" -exec grep -H "<string>" '{}' \; sau <sh/>grep -Hr "<string>" /path/

Fireste, <string> poate fi atat un sir de caractere cat si orice fel de expresie regulata suportata de versiunea particulara de grep disponibila pe masina in cauza.
Deasemenea, pentru a cauta fara a face diferenta intre majuscule si minuscule, adaugati parametrul -i la grep.

Daca doriti ca in output-ul comenzii grep expresia cautata sa fie cautata sa fie evidentiata puteti folosi parametrul --color astfel: <sh/>grep --color -Hr "<string>" /path/

Stergerea fisierelor backup

Asa cum stiti, prin traditie in *NIX, editoarele text fac o copie de siguranta fisierelor editate, copie de siguranta ce este denumita identic cu fisierul initial plus caracterul tilda (~). Ei bine... in cazul aplicatiilor web, acest backup mai mult dauneaza decat sa ajute. Nu ar fi prea placut sa aveti un index.php~ in DocumentRoot

<sh/>find /path -name "*~" -exec rm -f '{}' \;

Probleme cu spatiul pe disc?

Nu stiti unde "vi s-a dus" spatiul de pe disc si e cam greu sa verificati fiecare director in parte?
Linia urmatoare va ajuta, facand totodata si o sortare.

<sh/>du --max-depth=1 /path | sort -rn