CodeHint


Shell Script (15 items)



  Array example
declare -a LANGS=("PHP" "Shell" "Java" "CSS" "Javascript" "LUA")

for n in ${LANGS[@]}; do
   echo $n
done
  Edit
    #!/bin/bash

Bash declaration

  Edit
    while [ $x != $y ]; do
# While loop
while [ $x != "yes" ]; do
   echo "Say yes: " 
   read $x
done
  Edit
    until [ $x = $y ]; do
# Until loop (while inverse)
until [ $x = "yes" ]; do
   echo "Say yes: "
   read $x
done
  Edit
    ls notfound 2>> log

Appends error message to error descriptor 2 in file log

  Edit
    make > log 2>&1

Cloning output descriptor to error descriptor

  Edit
    echo $?
Exibe o status de saída do último comando
0 - Sucesso
1 - Erro geral
126 - Problema de permissão
127 - Comando não encontrado
130 - Comando cancelado por Control + C
  Edit
    shift

Rebaixa o valor de uma variável $3 para $2 por exemplo. E $1 para null

  Edit
    read -n1 -t 4 -p "Enter something" CHOICE

Read variable with instant enter, timer and label

  Edit
    $(command)
Returns results from a command to a variable.
CURRENT=$(pwd)
  Edit
    sleep 10

Delay in seconds in shell script

  Edit
sh --version

Checks sh version duhhh

  Edit
  Handling strings
${#var}Returns variable length
${!var}Eval variable
${var:N}Returns text since N
${var:N:F}Returns text from N to F
${var/t1/t2}Replaces t1 by t2 once
${var//t1/t2}Replaces t1 by t2 globally
  Edit
    if [ $x -eq $y ]; then
# Conditional example
if [ $x -lt $y ]; then
   echo "Minor thing"
fi

Numeric:
-lt (less than), -gt (greater than), -le (less equal), -ge (great equal)
-eq (equal), -ne (not equal)

String:
-z (null), -ne(not null), =, !=

Conditional:
! (not), -a (and), -o (or)

File:
-d (directory)
-f (file)
-e (file exists)
-G (group from current user ?)
-L (is a symbolic link ?)
-O (owner is current user?)
-r (can read), -w (can write), -x (can execute)
-nt (newer than), -ot (older than), -ef(equal file)
-s (filesize greater than)

  Edit
    case "$C" in
case "$C" in
'1')
    do_this
    ;;
[23])
    do_what_you_are_supposed_to_do
    ;;
*)
    do_nothing
    ;;
esac
  Edit