-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
124 changed files
with
5,428 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
# Este é o meu primeiro script | ||
msg="O meu segundo script em bash!" | ||
echo msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
# Este é o meu primeiro script | ||
msg="O meu primeiro script em bash!" | ||
echo $msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
# Atenção aos espaços | ||
echo Este e um teste | ||
echo "Este e um teste" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env -S bash -i | ||
# O comando type permite saber o tipo de um comando | ||
type rm | ||
type cd | ||
type for | ||
type ll | ||
type dequote |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env bash | ||
echo "Todos os items em /etc:" | ||
find /etc -maxdepth 1 -printf "%f\n" | ||
|
||
printf "\n" | ||
|
||
echo "Começam por a:" | ||
find /etc -maxdepth 1 -name "a*" -printf "%f\n" | ||
|
||
printf "\n" | ||
|
||
echo "Começam por a e têm mais que 3 caracteres": | ||
find /etc -maxdepth 1 -regextype egrep -regex ".*/a.{3,}" -printf "%f\n" | ||
|
||
printf "\n" | ||
|
||
echo "Contêm conf no nome": | ||
find /etc -maxdepth 1 -name "*conf*" -printf "%f\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env -S bash | ||
echo "O meu editor por omissão BASH $BASH \$BASH" | ||
echo 'O meu editor por omissão BASH $BASH \$BASH' | ||
echo $(( 5 + 5 )) | ||
(( 5 > 0 )) && echo "cinco é maior do que zero" | ||
today=$(date); echo $today |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
echo "$#" | ||
echo "Arg 1: $1" | ||
echo "Arg 2: $2" | ||
echo "$*" | ||
echo "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
echo using '$*:' $* | ||
for i in $*; do echo "$i"; done | ||
|
||
echo using '$@:' $@ | ||
for i in $@; do echo "$i"; done | ||
|
||
echo using '"$*":' "$*" | ||
for i in "$*"; do echo "$i"; done | ||
|
||
echo using '"$@":' "$@" | ||
for i in "$@"; do echo "$i"; done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
# Parameter Expansion | ||
file="$HOME/.bashrc" | ||
echo "File path: $file" | ||
echo "File name: ${file##*/}" | ||
echo "Directory name: ${file%/*}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
# Brace Expansion | ||
echo {1..9} | ||
echo {0,1}{0..9} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
# Exit status | ||
ping -c 1 www.ua.pt | ||
echo "Exit code: $?" # $? gives exit code of last process | ||
ping -c 1 wwwww.ua.pt | ||
echo "Exit code: $?" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
mkdir d && cd d && pwd | ||
echo "----------------" | ||
pwd && rm xpto || echo "I couldn't remove the file" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env bash | ||
touch "$1"{0..9}.dat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env bash | ||
touch xpto0{0..9}.dat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env bash | ||
file -E "$1" | ||
echo "Exit code: $?" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env bash | ||
chmod +x "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
if [[ "$#" -ne "2" ]]; then | ||
echo "Usage: $0 num1 num2" | ||
exit 1 | ||
fi | ||
|
||
echo $(("$1" + "$2")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env bash | ||
# Agrupamento de comandos na Bash | ||
{ | ||
i=0 | ||
while read line; do | ||
echo $i: $line | ||
i=$(($i+1)) | ||
done | ||
} < $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
# Conditional block if | ||
if $1; then | ||
echo "Verdadeiro" | ||
else | ||
echo "Falso" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
# Conditional block if | ||
if [[ $1 = $2 ]] ; then | ||
echo "O arg1 é igual ao arg2" | ||
else | ||
echo "Os args são diferentes" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
# Conditional block if | ||
if [ $1 = $2 ]; then | ||
echo "O arg1 é igual ao arg2" | ||
else | ||
echo "Os args são diferentes" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
# Conditional block if | ||
if [ "$1" = "$2" ]; then | ||
echo "O arg1 é igual ao arg2" | ||
else | ||
echo "Os args são diferentes" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
# Conditional block if | ||
if [ "$1" -gt 5 ] && [ "$1" -lt 10 ]; then | ||
echo "número maior do que 5 e menor do que 10" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env bash | ||
# This script checks the existence of a file | ||
|
||
if [ "$#" -ne 1 ]; then | ||
if [ "$#" -lt 1 ]; then | ||
>&2 echo "Error: Not enough arguments" | ||
else | ||
>&2 echo "Error: Too many arguments" | ||
fi | ||
|
||
>&2 echo "Usage: $0 file" | ||
exit 1; | ||
fi | ||
|
||
echo "Checking..." | ||
if [[ -f "$1" ]] ; then | ||
echo "$1 existe." | ||
else | ||
echo "$1 não existe" | ||
fi | ||
echo "...done." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env bash | ||
# This script checks the existence of a file | ||
|
||
if [ "$#" -ne 1 ]; then | ||
if [ "$#" -lt 1 ]; then | ||
>&2 echo "Error: Not enough arguments" | ||
else | ||
>&2 echo "Error: Too many arguments" | ||
fi | ||
|
||
>&2 echo "Usage: $0 file" | ||
exit 1; | ||
fi | ||
|
||
echo "Checking..." | ||
|
||
if ! [ -e "$1" ]; then | ||
echo "$1 não existe." | ||
exit 1 | ||
fi | ||
|
||
TYPE="unknown file" | ||
|
||
if [ -f "$1" ]; then TYPE="regular file" | ||
elif [ -b "$1" ]; then TYPE="block dev file" | ||
elif [ -c "$1" ]; then TYPE="char dev file" | ||
elif [ -d "$1" ]; then TYPE="directory" | ||
fi | ||
|
||
echo "$1 is a $TYPE" | ||
|
||
PERMS="" | ||
|
||
if [ -r "$1" ]; then PERMS="${PERMS}r"; else PERMS="${PERMS}-"; fi | ||
if [ -w "$1" ]; then PERMS="${PERMS}w"; else PERMS="${PERMS}-"; fi | ||
if [ -x "$1" ]; then PERMS="${PERMS}x"; else PERMS="${PERMS}-"; fi | ||
|
||
echo "$1 permissions: $PERMS" | ||
|
||
echo "...done." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
# Testa se ano dado (ou ano actual, se nenhum for dado) | ||
# é bissexto ou comum. | ||
if [[ $# = 1 ]]; then | ||
year=$1 | ||
else | ||
year=$(date +%Y) | ||
fi | ||
|
||
if [[ $(($year % 400)) -eq "0" ]]; then | ||
echo "Ano bissexto. Fevereiro tem 29 dias." | ||
elif [[ $(($year % 4)) -eq 0 ]]; then | ||
if [[ $(($year % 100)) -ne 0 ]]; then | ||
echo " Ano bissexto. Fevereiro tem 29 dias." | ||
else | ||
echo "Ano comum. Fevereiro tem 28 dias." | ||
fi | ||
else | ||
echo " Ano comum. Fevereiro tem 28 dias." | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env bash | ||
#This script does a very simple test for checking disk space. | ||
|
||
# `df -h`, report file system space usage in human readable format | ||
# `awk '{print $5}'` - Print the fifth column (white space separated columns) | ||
# `grep %` - Remove lines without the % symbol | ||
# `grep -v Use` - Remove lines with the `Use` word | ||
# `sort -n` - Sort lines numerically | ||
# `tail -1` - Select the last line | ||
# `cut -d "%" -f1 -` - Run the command `cut` reading from stdin (`-`) with the delimiter % | ||
# and selecting the first field. | ||
space=$(df -h | awk '{print $5}' | grep % | grep -v use | sort -n \ | ||
| tail -1 | cut -d "%" -f1 - | awk '{printf("%02d\n", $1)}') | ||
|
||
echo "largest occupied space = $space%" | ||
case $space in | ||
[0-6][0-9]) # espaço < 70% | ||
Message="All OK." | ||
;; | ||
[7-8][0-9] ) # 70% <= espaço < 90% | ||
Message="Cleaning out. One partition is $space % full." | ||
;; | ||
9[0-8] ) # 90% <= espaço < 99% | ||
Message="Better buy a new disk. One partition is $space % full." | ||
;; | ||
99 ) # espaço = 99% | ||
Message="I'm drowning here! There's a partition at $space %!" | ||
;; | ||
* ) | ||
Message="I seem to be running with a non-existent disk..." | ||
;; | ||
esac | ||
echo $Message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env bash | ||
#This script does a very simple test for checking disk space. | ||
|
||
# `df -h`, report file system space usage in human readable format | ||
# `awk '{print $5}'` - Print the fifth column (white space separated columns) | ||
# `grep %` - Remove lines without the % symbol | ||
# `grep -v Use` - Remove lines with the `Use` word | ||
# `sort -n` - Sort lines numerically | ||
# `tail -1` - Select the last line | ||
# `cut -d "%" -f1 -` - Run the command `cut` reading from stdin (`-`) with the delimiter % | ||
# and selecting the first field. | ||
space=$(df -h | awk '{print $5}' | grep % | grep -v Use | sort -n \ | ||
| tail -1 | cut -d "%" -f1 -) | ||
|
||
echo "largest occupied space = $space%" | ||
|
||
largest=$(df | awk '{print $2 " " $1}' | tail -n +2 | sort -n -k1 \ | ||
| tail -1 | awk '{print $2 ": " $1}' | numfmt --field=2 --to=iec --from-unit=1024 ) | ||
|
||
echo "Largest partition $largest" | ||
case $space in | ||
[0-6][0-9]) # espaço < 70% | ||
Message="All OK." | ||
;; | ||
[7-8][0-9] ) # 70% <= espaço < 90% | ||
Message="Cleaning out. One partition is $space % full." | ||
;; | ||
9[0-8] ) # 90% <= espaço < 99% | ||
Message="Better buy a new disk. One partition is $space % full." | ||
;; | ||
99 ) # espaço = 99% | ||
Message="I'm drowning here! There's a partition at $space %!" | ||
;; | ||
* ) | ||
Message="I seem to be running with a non-existent disk..." | ||
;; | ||
esac | ||
echo $Message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env bash | ||
shopt -s extglob | ||
|
||
case "$1|$2" in | ||
?([0-9])[0-9]"|"sec*) | ||
echo "Válido." | ||
;; | ||
* ) | ||
echo "Não é válido" | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
# For all the files in a folder, show their properties | ||
for f in $1/*; do | ||
file "$f" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env bash | ||
if [ "$#" -ne 1 ]; then | ||
if [ "$#" -lt 1 ]; then | ||
>&2 echo "Error: Not enough arguments" | ||
else | ||
>&2 echo "Error: Too many arguments" | ||
fi | ||
|
||
>&2 echo "Usage: $0 dir" | ||
exit 1; | ||
fi | ||
# For all the files in a folder, show their properties | ||
for f in $1/*; do | ||
file "$f" | ||
done |
Oops, something went wrong.