Created
March 9, 2015 13:05
-
-
Save AlejandroPerezMartin/52688e9cd3ad57429ab7 to your computer and use it in GitHub Desktop.
LFS101x.2 Introduction to Linux - Notes
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
#!/bin/bash | |
# Declaring functions | |
function display() { | |
echo "Hello" $1 "!" | |
} | |
# ./my_script.sh param1 param2 | |
# $0 (script name) | |
# $1 .. $n (nth parameter) | |
# $* (all parameters) | |
# $# (number of arguments) | |
# Using variables | |
var="something" | |
echo $var | |
# Read user input | |
echo "Please enter your name:" | |
read name | |
display $name # call to function | |
# Testing for files | |
if [[ -f /etc/passwd ]] | |
then | |
echo "The file exists!" | |
elif [[ -f /etc/other ]] | |
then | |
echo "Other file exists" | |
else | |
echo "The file doesn't exist" | |
fi | |
# Operands: | |
# &&, ||, ! (not) | |
# Testing for files: | |
# ----------------- | |
# -e (file exists) | |
# -d (file is directory) | |
# -f (file is regular file) | |
# -s (file has non-zero size) | |
# -r (file is readable) | |
# -w (file is writable) | |
# -x (file is executable) | |
# Numerical tests: | |
# ---------------- | |
# -eq, ne, gt, lt, ge, le | |
echo $((2 + 1)) | |
echo $? # status of the last command | |
string="alejandro" | |
len=${#string} # Saves the length of string1 in the variable len | |
halfLength=${string:0:4} | |
echo $len | |
echo $halfLength | |
set -x # start debugging | |
read NUM | |
case $NUM in | |
1) echo "one";; | |
2) echo "two";; | |
3) var=$NUM; exit;; | |
*) echo "other number";; | |
esac | |
set +x # stop debugging | |
TEMP=$(mktemp /tmp/tempfile.XXXXX) # create temporary file | |
# add option -d to create a folder | |
# command: test | |
# exit # exit status of the last command, equivalent to | |
# command below | |
# exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment