Herramientas de usuario

Herramientas del sitio


wiki2:linux_scripting

¡Esta es una revisión vieja del documento!


Tabla de Contenidos

Linux Programming

Usar la salida del último comando (volviendolo a ejecutar):

$ > echo pierre
pierre
$ > echo my name is $(!!)
echo my name is $(echo pierre)
my name is pierre

Exit status del último comando:

$ touch /root/test
touch: cannot touch '/root/test': Permission denied
$ echo $?
1

Añadir la salida de un comando como parámetro a otro

$ ls $(echo ~)

Guardarlo en una variable

alfred@alfred-Y50-70 ~/Desktop $ a=$(python crawler.py)
alfred@alfred-Y50-70 ~/Desktop $ echo $a

Run with root:

if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

Si quieres que el script pare cuando haya un error interno añade la siguiente línea al inicio de este:

set -e

Fast notes

Wait to press a key:

read -n1 -r -p "Press any key to continue..." key;

Examples

Some Django shortcuts:

resetmigrations() {
  if [ -z "$1" ]
    then
      echo "No database supplied"
      return
  fi
  echo "The $1 database is going to be reseted";
  read -n1 -r -p "Press any key to continue..." key;
  find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
  find . -path "*/migrations/*.pyc"  -delete
  export PGPASSWORD=mysecretpassword;
  dropdb -h 127.0.0.1 -U postgres $1
  createdb -T template0 -h 127.0.0.1 -U postgres $1
  python manage.py makemigrations
  python manage.py migrate
}
wiki2/linux_scripting.1552037763.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)