Skip to content

Instantly share code, notes, and snippets.

@HiroNakamura
Last active June 11, 2023 22:02
Show Gist options
  • Save HiroNakamura/c3698a67afd056b13193244d97874739 to your computer and use it in GitHub Desktop.
Save HiroNakamura/c3698a67afd056b13193244d97874739 to your computer and use it in GitHub Desktop.
Snippets

Snippets

Ejemplos en Linux Bash y otros lenguajes

Leer una entrada

echo -n "Proceed? [y/n]: "
read -r ans
echo "$ans"

Condicionales

if ping -c 1 google.com; then
  echo "It appears you have a working internet connection"
fi
if grep -q 'ping' ~/.bash_history; then
  echo "Tu escribiste 'ping' alguna vez."
fi
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
  -V | --version )
    echo "$version"
    exit
    ;;
  -s | --string )
    shift; string=$1
    ;;
  -f | --flag )
    flag=1
    ;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi

Case switch

case "$1" in
  start | up)
    vagrant up
    ;;

  *)
    echo "Usage: $0 {start|stop|ssh}"
    ;;
esac

Realizar operaciones

$((a + 200))      # Add 200 to $a

$(($RANDOM%200))  # Random number 0..199

declare -i count  # Declare as type integer 
count+=1          # Increment
# String
if [[ -z "$string" ]]; then
  echo "String is empty"
elif [[ -n "$string" ]]; then
  echo "String is not empty"
else
  echo "This never happens"
fi

# Combinations
if [[ X && Y ]]; then
  ...
fi

# Equal
if [[ "$A" == "$B" ]]

# Regex
if [[ "A" =~ . ]]

if (( $a < $b )); then
   echo "$a is smaller than $b"
fi

if [[ -e "file.txt" ]]; then
  echo "file exists"
fi
Parámetro Significado
$? Exit status of last task
$! PID of last background task
$$ PID of shell
$0 Filename of the shell script
$_ Last argument of the previous command
${PIPESTATUS[n]} return value of piped commands (array)
Instruccón Significado
[[ -e FILE ]] Exists
[[ -r FILE ]] Readable
[[ -h FILE ]] Symlink
[[ -d FILE ]] Directory
[[ -w FILE ]] Writable
[[ -s FILE ]] Size is > 0 bytes
[[ -f FILE ]] File
[[ -x FILE ]] Executable
[[ FILE1 -nt FILE2 ]] 1 is more recent than 2
[[ FILE1 -ot FILE2 ]] 2 is more recent than 1
[[ FILE1 -ef FILE2 ]] Same files
[[ -o noclobber ]] If OPTIONNAME is enabled
[[ ! EXPR ]] Not
[[ X && Y ]] And
[[ X or Y ]] Or
[[ -z STRING ]] Empty string
[[ -n STRING ]] Not empty string
[[ STRING == STRING ]] Equal
[[ STRING != STRING ]] Not Equal
[[ NUM -eq NUM ]] Equal
[[ NUM -ne NUM ]] Not equal
[[ NUM -lt NUM ]] Less than
[[ NUM -le NUM ]] Less than or equal
[[ NUM -gt NUM ]] Greater than
[[ NUM -ge NUM ]] Greater than or equal
[[ STRING =~ STRING ]] Regexp
(( NUM < NUM )) Numeric conditions

Enlaces:

//el archivo JSON lo convierto a un array y lo guardo en la variable series
var series;
//funcion que lee mi archivo JSON
function loadData(){
let data = JSON.parse(fs.readFileSync(path.resolve('data','series.json'),'utf8' ,(err,data) => {
if (err){console.error(err); return;}
return data;
}));
series = data.series;
}
//Cuando guardo los cambios se dispara la función save que recibe como párametro el id
//de la serie
function save(index) {
//Modifico los datos directamente en el array
series[index] = {
titulo: `${document.getElementById('titulo').value}`,
descripcion: `${document.getElementById('descripcion').value}`,
fecha_emision: `${document.getElementById('fecha_emision').value}`,
imagen: `${document.getElementById('imagen').getAttribute('src')}`
};
//creo una variable con un objeto que tiene la estructura del archvo JSON
let jsonData = {series : series};
//con la clase fs sobreescribo el archivo ya con los cambios hechos
fs.writeFileSync(path.resolve('data','series.json'), JSON.stringify(jsonData), (err) => {
if (err) throw err;
});
//metodo para cerrar un modal en materialize css
instances.close();
//mando llamar mi fucnion load que es la que vuelve a llenar mis variables globales
//como la variable series
loadData();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment