Lesson 06 - chad-p/wiki-linux-class GitHub Wiki
Bash Scripting
Alias
Bash Scripting
- https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html
- https://www.redhat.com/sysadmin/formatting-date-command
- https://www.networkworld.com/article/2694433/unix-good-coding-practices-for-bash.html
- https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html
- https://tldp.org/LDP/abs/html/options.html
- https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Conditional-Constructs
echo “What is your favorite dish?”
read dish
echo “I like $dish also!”
#!/bin/bash
WHO_AM_I=$(whoami)
LIST=`ls -a`
echo $WHO_AM_I; echo $LIST
Logical Operators
- AND = Please go to the store AND purchase some milk
- OR = Please go to bed OR you will be grounded.
#!/bin/bash
num=3
read -ep "Enter a number: " userNum
if [ $userNum -gt $num ]; then
echo "Your number is greater than the number in the variable"
else
echo "The number in the variable is greater or equal to your number"
fi
#!/bin/bash
for i in {1..255};
do
echo "i var is: $i"
done
#!/bin/bash
# Clear file at the beginning of the process
cd ~/Desktop
echo "" > liveHost;
# Loop in the range 1 to 255
for i in {1..255};
do
echo "[+] pinging 10.71.0.$i";
ping -c 1 10.71.0.$i | grep "bytes from" >> liveHost;
done
echo "Done!"
#!/bin/bash
x=1
#A loop that will run as long as the value of $x is less than 5
while [ $x -lt 5 ]
do
echo “Execution times count: $x”
x=$(( $x + 1 ))
done
test 2 -gt 3; echo $?
#!/bin/bash
touch /root/file.txt
if [ $? -lt 1 ]
then
echo “File created."
else
echo “An error has occurred."
fi
#!/bin/bash
for i in {1..255};
do
let sum=$i*$i
echo "$i multiplied by itself equals: $sum"
done
Arithmetic
- https://phoenixnap.com/kb/bash-math#ftoc-heading-16
- https://askubuntu.com/questions/939294/difference-between-let-expr-and
y=5
x=10
let sum=2+2
echo "This is the sum $sum" # This is the sum 4
let sum+=2
echo $sum
expr $x+$y # > 5+10
expr $x + $y # > 15
expr 5 * 5 # error because * needs to be \*
expr 11 / 5 # Still floor division
echo $x+$y # > 5+10
echo $(($x+$y)) # > 15
echo $((x+y)) # > 15
echo $((x/y)) # > Floor result... Update x to 11 and rerun
expr $x+$y # > 5+10
expr $x + $y # > 15
expr 11 / 5 # Still floor division
echo "1 / 5" | bc
echo "scale=2; 11 / 5" | bc # bc stands for basic calculator
Tar
-
https://www.interserver.net/tips/kb/use-tar-command-linux-examples/
-
-
[c]reate an archive and write it to a [f]ile:
tar cf target.tar file1 file2 file3
-
[c]reate a g[z]ipped archive and write it to a [f]ile:
tar czf target.tar.gz file1 file2 file3
-
[c]reate a g[z]ipped archive from a directory using relative paths:
tar czf target.tar.gz --directory=path/to/directory .
-
E[x]tract a (compressed) archive [f]ile into the current directory [v]erbosely:
tar xvf source.tar[.gz|.bz2|.xz]
-
E[x]tract a (compressed) archive [f]ile into the target directory:
tar xf source.tar[.gz|.bz2|.xz] --directory=directory
-
[c]reate a compressed archive and write it to a [f]ile, using [a]rchive suffix to determine the compression program:
tar caf target.tar.xz file1 file2 file3
-
Lis[t] the contents of a tar [f]ile [v]erbosely:
tar tvf source.tar
-
E[x]tract files matching a pattern from an archive [f]ile:
tar xf source.tar --wildcards "*.html"
-
bzip2/bunzip2 - A block-sorting file compressor.
-
Compress a file:
bzip2 path/to/file_to_compress
-
Decompress a file:
bzip2 -d path/to/compressed_file.bz2
-
Decompress a file to standard output:
bzip2 -dc path/to/compressed_file.bz2
gzip
-
Compress a file, replacing it with a gzipped compressed version:
gzip file.ext
-
Decompress a file, replacing it with the original uncompressed version:
gzip -d file.ext.gz
-
Compress a file, keeping the original file:
gzip --keep file.ext
-
Compress a file specifying the output filename:
gzip -c file.ext > compressed_file.ext.gz
-
Decompress a gzipped file specifying the output filename:
gzip -c -d file.ext.gz > uncompressed_file.ext
-
Specify the compression level. 1=Fastest (Worst), 9=Slowest (Best), Default level is 6:
gzip -9 -c file.ext > compressed_file.ext.gz
zip
zip -e “locked.zip” file1 file2 file3
or --encrypt
-
Add files/directories to a specific archive:
zip -r path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
-
Remove files/directories from a specific archive:
zip --delete path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
-
Archive files/directories e[x]cluding specified ones:
zip path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ... --exclude path/to/excluded_files_or_directories
-
Archive files/directories with a specific compression level (
0
- the lowest,9
- the highest):zip -r -0-9 path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
-
Create an encrypted archive with a specific password:
zip -r --encrypt path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
-
Archive files/directories to a multi-part [s]plit zip file (e.g. 3 GB parts):
zip -r -s 3g path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
-
Print a specific archive contents:
zip -sf path/to/compressed.zip
xz
-
Compress a file to the xz file format:
xz file
-
Decompress a xz file:
xz -d file.xz
-
Compress a file to the LZMA file format:
xz --format=lzma file
-
Decompress an LZMA file:
xz -d --format=lzma file.lzma
-
Decompress a file and write to stdout:
xz -dc file.xz
-
Compress a file, but don't delete the original:
xz -k file
-
Compress a file using the fastest compression:
xz -0 file
-
Compress a file using the best compression:
xz -9 file
Hasing
- https://codesigningstore.com/hash-algorithm-comparison
- https://www.redhat.com/sysadmin/hashing-checksums