snippets / bash

All snippets tagged bash (6)

  1. raccourcis bash

    plein de raccourcis pour bash, crédit : Benoît BALON (posteet)

     1 tab   <--->   auto complétion pour les fichiers et les dossiers
    2 ^[chaîne 1]^[chaîne 2]^ <---> lance la dernière commande en remplaçant la chaîne 1 par la chaîne 2
    3 !-[n] <---> rappelle la commande lancée il y a n commandes
    4 !! <---> lance la dernière commande
    5 !?[chaîne] <---> lance la dernière commande terminant par la chaîne de caractères
    6 !?[chaîne]? <---> lance la dernière commande contenant la chaîne de caractères
    7 ![chaîne] <---> lance la dernière commande commençant par la chaîne de caractères
    8 ![] <---> rappelle la commande n°... de l'historique
    9 !# <---> lance la plus ancienne commande de l'historique
    10 [commande] !^ <---> lance la commande avec le premier argument de la commande précédente
    11 [commande] !:[n]-[m] <---> lance la commande avec les arguments n à m de la commande précédente
    12 [commande] !:[] <---> lance la commande avec l'argument n°... de la commande précédente
    13 [commande] !$ <---> lance la commande avec le dernier argument de la commande précédente
    14 Alt + . <---> colle le dernier mot de la ligne précédente
    15 Alt + b <---> déplace le curseur d'un mot vers la gauche
    16 Alt + c <---> met en majuscule la lettre courante, en minuscules les autres lettres du mot courant, puis se place au mot suivant
    17 Alt + d <---> efface le mot suivant
    18 Alt + f <---> déplace le curseur d'un mot vers la droite
    19 Alt + t <---> échange le mot courant et le mot précédent
    20 Ctrl + _ <---> Annuler les dernières modifications (rester appuyé)
    21 Ctrl + a <---> déplace le curseur en début de ligne
    22 Ctrl + c <---> envoie le signal SIGINT au processus en cours
    23 Ctrl + d <---> efface le caractère courant, ou déconnecte (logout) si la ligne est déjà vide
    24 Ctrl + e <---> déplace le curseur en fin de ligne
    25 Ctrl + h <---> efface le dernier caractère
    26 Ctrl + k <---> coupe tout à droite du curseur
    27 Ctrl + l <---> efface l'écran (commande clear)
    28 Ctrl + n <---> commande suivante (équivalent à la touche flèche bas)
    29 Ctrl + o <---> exécute la commande (touche entrée)
    30 Ctrl + p <---> commande précédente (équivalent à la touche flèche haut)
    31 Ctrl + q <---> fait apparaître la saisie / relance l'affichage
    32 Ctrl + r <---> recherche dans l'historique (Ctrl + r pour remonter à la chaîne précédente)
    33 Ctrl + s <---> masque la saisie / arrête l'affichage (touche pause)
    34 Ctrl + t <---> permet d'inverser deux lettres
    35 Ctrl + u <---> coupe tout à gauche du curseur
    36 Ctrl + w <---> coupe le mot à gauche du curseur
    37 Ctrl + y <---> colle ce qui a été effacé à gauche du curseur (suite à un Ctrl + u)
    38 Ctrl + z <---> passe le processus en cours en arrière plan
    39 Echap, 10, A <---> répète 10 fois le caractère A
    40 Echap, 5, Ctrl + q, Ctrl + v, 9 <---> répète 5 fois le chiffre 9
    Posted by ismael to shell bash raccourcis ... saved by 1 person ... 2 comments ... 5 months, 4 weeks
  2. Rotate backup files (mysql - mysqlhotcopy)

    Makes backup named 1_dump.tar.gz, 2_dump.tar.gz... You'll get as many backup as $ROT_PERIOD. In this example, you have backup history for 15 days. Date is stored via extracted tgz dir name.

     1 #!/bin/bash
    2
    3 ################################################
    4 # author : Alexandre BULTE - alexandre[at]bulte[dot]net
    5 # license : GPL v2
    6 ################################################
    7
    8 BACKUP_PATH='/home/xxx/backup'
    9 # db names
    10 DBS='db1 db2 db3'
    11 MYSQL_USER='root'
    12 MYSQL_PASSWD='xxxxxx'
    13 ROT_PERIOD=15
    14 NEXT_ID=0
    15 DATE_TODAY=$( date +%Y%m%d-%H%M%S )
    16
    17 # rep sauvegarde
    18 REP=$BACKUP_PATH/$DATE_TODAY
    19 if [ ! -d $REP ];
    20 then
    21 mkdir $REP
    22 fi
    23
    24 # fetch biggest id in dir
    25 cd $BACKUP_PATH
    26 BIG_ID=$( ls -1 *_dump.tar.gz | tail -n 1 | cut -d '_' -f 1 ) &> /dev/null
    27
    28 if [ ! $BIG_ID ];
    29 then
    30 BIG_ID=0
    31 fi
    32
    33 # rotation if at least 1_dump.tar.gz
    34 for i in $( seq 1 $BIG_ID );
    35 do
    36 NEXT_ID=$( expr $i + 1 )
    37 NEXT_FILENAME=$NEXT_ID'_dump.tar.gz'
    38 FILENAME=$i'_dump.tar.gz'
    39 if [ -e $FILENAME ];
    40 then
    41 echo "$FILENAME exists"
    42 if [ $i = $ROT_PERIOD ];
    43 then
    44 echo "Removing oldest archive..."
    45 rm $i'_dump.tar.gz'
    46 else
    47 echo "Rotating $i..."
    48 cp $FILENAME $NEXT_FILENAME
    49 fi
    50 fi
    51 done
    52
    53 # sauvegarde
    54 mysqlhotcopy -q -u $MYSQL_USER -p $MYSQL_PASSWD $DBS $REP
    55
    56 # compression
    57 tar cfz 1_dump.tar.gz $DATE_TODAY
    58
    59 # suppression
    60 rm -rf $DATE_TODAY
    Posted by babeloued to shell mysql mysqlhotcopy rotate backup ... saved by 3 persons ... 0 comments ... 8 months, 2 weeks
  3. pkgsearch - Search archlinux pkgbuild

    pkgsearch list all packages that contain your string and display installed packages. pksearch -h for options

      1 #!/bin/bash
    2 #
    3 # pkgsearch
    4 #
    5 # Copyright (c) 2006 by Benoit Chesneau <benoit@bchesneau.info>
    6 #
    7 # This program is free software; you can redistribute it and/or modify
    8 # it under the terms of the GNU General Public License as published by
    9 # the Free Software Foundation; either version 2 of the License, or
    10 # (at your option) any later version.
    11 #
    12 # This program is distributed in the hope that it will be useful,
    13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    15 # GNU General Public License for more details.
    16 #
    17 # You should have received a copy of the GNU General Public License
    18 # along with this program; if not, write to the Free Software
    19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    20 # USA.
    21 #
    22 # AUTHORS :
    23 # - Maintainer : Benoit Chesneau <benoitc@archlinuxfr.org>
    24 # - Contributor : slubman <slubman@archlinuxfr.org>
    25 # CHANGELOG :
    26 # - version 0.6
    27 # cosmethics changes
    28 # internal rewrite
    29 # - version 0.5 (2007-02-28 15:19) :
    30 # search on multiple words (slubman)
    31 # search in description is case insensitive (slubman)
    32 # - version 0.4 (2007-02-28 13:14) :
    33 # add possibility to search in repository (slubman)
    34 # add possibility to exclude repository in search (slubman)
    35 # - version 0.3 (2007-02-28) :
    36 # add possibility to display only installed packages (slubman)
    37 # add possibility to display only not installed packages (benoitc)
    38 # - version 0.1
    39 # initial release
    40 #
    41
    42 PACMAN_SOURCES="/var/lib/pacman"
    43 PWD=`pwd`
    44
    45 cd $PACMAN_SOURCES
    46
    47 myver="0.6"
    48
    49
    50 NAME_ONLY=0
    51 USE_COLOR="y"
    52 NOT_INSTALLED=0
    53 ONLY_INSTALLED=0
    54 IN="all packages"
    55 SKIP_REPO=()
    56 IN_REPO=()
    57 search_string=()
    58
    59 installed() {
    60 if [ "$USE_COLOR" = "Y" -o "$USE_COLOR" = "y" ]; then
    61 echo -e "\033[1;35m$1\033[1;0m \033[1;35m(*)\033[1;0m" >&2
    62 else
    63 echo "$1 (*)" >&2
    64 fi
    65 let nb=nb+1
    66 }
    67
    68 not_installed() {
    69 echo "$1" >&2
    70 let nb=nb+1
    71 }
    72
    73 msg() {
    74 if [ "$USE_COLOR" = "Y" -o "$USE_COLOR" = "y" ]; then
    75 echo -e "\033[1;32m==>\033[1;0m \033[1;1m$1\033[1;0m" >&2
    76 else
    77 echo "==> $1" >&2
    78 fi
    79 }
    80
    81 error() {
    82 if [ "$USE_COLOR" = "Y" -o "$USE_COLOR" = "y" ]; then
    83 echo -e "\033[1;31m==> ERROR:\033[1;0m \033[1;1m$1\033[1;0m" >&2
    84 else
    85 echo "==> ERROR: $1" >&2
    86 fi
    87 }
    88
    89 usage() {
    90 echo "pkgsearch $myver"
    91 echo "usage : $0 [-n] <searchstring>"
    92 echo
    93 echo "pkgsearch list all packages that contain your"
    94 echo "string and display installed packages"
    95 echo
    96 echo "Options"
    97 echo " -n, --name Search on pkgname only"
    98 echo " -m, --nocolor Disable colorized output messages"
    99 echo " -i, --installed Display only installed packages"
    100 echo " -sr, --skip-repo Exclude repo in search"
    101 echo " -ir, --ni-repo Search only in repo"
    102 echo " -s, --skip Display only not installed packages"
    103 echo " -h, --help Display usage"
    104 }
    105
    106 if [ $# -lt 1 ]; then
    107 usage
    108 exit 1
    109 fi
    110
    111 while [ "$#" -ne "0" ]; do
    112 case $1 in
    113 --installed|-i)
    114 ONLY_INSTALLED=1
    115 IN="only installed packages"
    116 ;;
    117 --help|-h)
    118 usage
    119 exit 0
    120 ;;
    121 --name|-n)
    122 NAME_ONLY=1
    123 ;;
    124 --nocolor|-m)
    125 USE_COLOR=n
    126 ;;
    127 --skip|-s)
    128 NOT_INSTALLED=1
    129 IN="only not installed packages"
    130 ;;
    131 --skip-repo|-sr)
    132 SKIP_REPO[${#SKIP_REPO[*]}]=$2
    133 shift
    134 ;;
    135 --in-repo|-ir)
    136 IN_REPO[${#IN_REPO[*]}]=$2
    137 shift
    138 ;;
    139 *)
    140 search_string[${#search_string[*]}]=$1
    141 ;;
    142 esac
    143 shift
    144 done
    145
    146 if [ "$NOT_INSTALLED" = "1" -a "$ONLY_INSTALLED" = "1" ]; then
    147 error "Error : Skip or show installed packages, but not both..."
    148 exit 0
    149 fi
    150
    151 nb=0
    152
    153 msg "Looking for \""${search_string[*]:-nothing}\""\n\tin ${IN}\n\tof ${IN_REPO[*]:-all repos}\n\texcluding ${SKIP_REPO[*]:-no repo}..."
    154 search_token=${search_string[0]}
    155 unset search_string[0]
    156 for word in ${search_string[*]}; do
    157 search_token="$search_token|$word"
    158 done
    159 search_token="($search_token)"
    160
    161 for repo in `find * -maxdepth 0 -type d`; do
    162 if ! [ $repo = 'local' ]; then
    163 if [ "$(echo ${SKIP_REPO[@]} | grep $repo)" ]; then
    164 continue
    165 fi
    166 if [ "$(echo ${IN_REPO[@]} | grep -v $repo)" ]; then
    167 continue
    168 fi
    169 cd $repo
    170 for pkg in `/bin/ls`; do
    171 if [ -f $pkg/desc ]; then
    172 if [ "$NAME_ONLY" = "1" ]; then
    173 if [ "$(echo $pkg | grep -E $search_token)" ]; then
    174 NAME=`grep -A 1 NAME $pkg/desc | tail -1`
    175 VERSION=`grep -A 1 VERSION $pkg/desc | tail -1`
    176 if [ -d $PACMAN_SOURCES/local/$pkg ]; then
    177 if [ "$NOT_INSTALLED" = "0" ]; then
    178 installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
    179 fi
    180 else
    181 if [ "$ONLY_INSTALLED" = "0" ]; then
    182 not_installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
    183 fi
    184 fi
    185 fi
    186 else
    187 if [ "$(grep -E -i $search_token $pkg/desc)" ]; then
    188 NAME=`grep -A 1 NAME $pkg/desc | tail -1`
    189 VERSION=`grep -A 1 VERSION $pkg/desc | tail -1`
    190 if [ -d $PACMAN_SOURCES/local/$pkg ]; then
    191 if [ "$NOT_INSTALLED" = "0" ]; then
    192 installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
    193 fi
    194 else
    195 if [ "$ONLY_INSTALLED" = "0" ]; then
    196 not_installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
    197 fi
    198 fi
    199 fi
    200 fi
    201 fi
    202 done
    203 cd ..
    204 fi
    205 done
    206
    207 echo
    208 echo "$nb packages found.."
    209
    210 cd $PWD
    Posted by benoitc to shell archlinux packages bash ... saved by 2 persons ... 0 comments ... 9 months
  4. Images resizing with shell and imagemagick

    Resize a lot of images with shell and imagemagick

    1 for i in *.jpg
    2 do convert $i -scale 1024x760 resized-$i;
    3 done
    Posted by comete to shell bash images resize shell ... saved by 5 persons ... 1 comments ... 9 months, 3 weeks
  5. upload images to imageshack from console

    Script to upload images to imageshack from console. Require bash and curl.

     1 #!/bin/bash
    2 #
    3 # imageshack_upload.sh
    4 #
    5 # Copyright (c) 2007 by enki <enki@crocobox.org>
    6 #
    7 # This program is free software; you can redistribute it and/or modify
    8 # it under the terms of the GNU General Public License as published by
    9 # the Free Software Foundation; either version 2 of the License, or
    10 # (at your option) any later version.
    11 #
    12 # This program is distributed in the hope that it will be useful,
    13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    15 # GNU General Public License for more details.
    16 #
    17 # You should have received a copy of the GNU General Public License
    18 # along with this program; if not, write to the Free Software
    19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    20 # USA.
    21 #
    22 # Changelog :
    23 # * 2006/10/22 - Updated to support files with space .
    24 # Changes proposed by slubman(http://www.slubman.net)
    25 # * 2006/10/22 - First version
    26
    27 myver='0.2'
    28 CURL=$(which curl)
    29
    30
    31
    32 TMPFILE=$(mktemp /tmp/imageshack.XXXXXXXXXX) || exit 1
    33
    34 cleanup() {
    35 rm -rf $TMPFILE
    36 [ "$1" ] && exit $1
    37 }
    38
    39 usage() {
    40 echo "imageshack_upload $myver"
    41 echo "usage: $0 <root>"
    42 echo
    43 echo "This script allow you to send image to htpp://www.imageshack.us"
    44 echo "in command line"
    45 echo
    46 echo "example: imageshack_upload.sh `pwd`/myimage.png"
    47 echo
    48 }
    49
    50 if [ $# -lt 1 ]; then
    51 usage
    52 rm -rf $TMPFILE
    53 exit 1
    54 fi
    55
    56 if [ "$1" = "-h" -o "$1" = "--help" ]; then
    57 usage
    58 rm -rf $TMPFILE
    59 exit 0
    60 fi
    61
    62
    63 img="$1"
    64
    65 if ! [ -f "$img" ]; then
    66 echo "Error: file don't exist"
    67 exit 1
    68 fi
    69
    70 $CURL -H Expect: -F fileupload="@${img}" -F xml=yes http://www.imageshack.us/index.php > $TMPFILE
    71
    72 echo "Url of image on imageshack:"
    73 echo $(cat $TMPFILE | grep -E "<image_link>(.*)</image_link>" | sed 's|<image_link>\(.*\)</image_link>|\1|')
    74
    75 cleanup
    Posted by enki to shell curl bash imageshack upload ... saved by 6 persons ... 0 comments ... 9 months, 3 weeks
  6. class to parse bash variable from a script

    This class allow you to parse variable in a bash script from ruby . It retourn an array of variables. bash array is also saved as ruby array.

     1 #!/usr/bin/ruby
    2 #
    3 # how to use it :
    4 # ovars=EnvBash.new(file)
    5 # bash_vars=ovars.vars
    6 #
    7
    8
    9 class MultiRegexp < Regexp
    10 def matches(str)
    11 str.scan(self) do
    12 yield Regexp.last_match
    13 end
    14 end
    15 end
    16
    17
    18 class EnvBash
    19 attr_reader :bash_env
    20
    21 def initialize(source)
    22 bash_script ="\
    23 source #{source}
    24 set
    25 "
    26 @bash_env = `#{bash_script}`
    27 end
    28
    29 def vars
    30 bvars=Hash.new
    31 re=MultiRegexp.new('(\w*)=(.*)$', true)
    32 re.matches(@bash_env) { |i|
    33 if (i[1] != "")
    34 if i[2] =~ /\(([^\)]*)\)/
    35 ta=Array.new
    36 re_a=MultiRegexp.new('\[\d*\]=\"([^"]*)\"', true)
    37 re_a.matches($1) { |j|
    38 ta.push(j[1])
    39 }
    40 bvars[i[1]]=ta
    41 elsif
    42 bvars[i[1]]=i[2]
    43 end
    44 end
    45 }
    46 return bvars
    47 end
    48 end
    49
    50 # test on a pkgbuild (archlinux)
    51 bashvars=EnvBash.new("/var/abs/kernels/kernel26/PKGBUILD")
    52
    53 bvars=bashvars.vars
    54 puts "#{bvars['pkgname']}-#{bvars['pkgver']}-#{bvars['pkgrel']}"
    55
    56 if bvars['source'].instance_of?(Array)
    57 puts "sources : "
    58 bvars['source'].each { |src|
    59 puts src
    60 }
    61 end
    Posted by enki to ruby bash variables parser ... saved by 1 person ... 0 comments ... 9 months, 3 weeks
showing 10, 25, 50 items per pages

Pages : 1

Flux RSS friendsnippetLatest snippets


More...