mirror of
https://github.com/autistic-symposium/shell-whiz-toolkit.git
synced 2025-05-12 11:42:20 -04:00
more bash examples
This commit is contained in:
parent
68e3f7d899
commit
4b0a965e4a
9 changed files with 199 additions and 0 deletions
53
shell_scripts/lessons/TRICKS.md
Normal file
53
shell_scripts/lessons/TRICKS.md
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
Wild Cards
|
||||||
|
----------
|
||||||
|
|
||||||
|
Use *, ? (for one char), and [abc] for a series of options. Example:
|
||||||
|
|
||||||
|
$ ls [abc]*
|
||||||
|
|
||||||
|
|
||||||
|
More commands on one line
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
Use ;
|
||||||
|
$ date;who
|
||||||
|
|
||||||
|
|
||||||
|
Redirection of Standard output/input
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
>, >>, <
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
$ sort < filename > sorted_names
|
||||||
|
|
||||||
|
|
||||||
|
Pipes
|
||||||
|
-----
|
||||||
|
A pipe is nothing but a temporary storage place where the output of one command is stored and then passed as the input for second command.
|
||||||
|
|
||||||
|
|
||||||
|
Processes and PID
|
||||||
|
-----------------
|
||||||
|
An instance of running command is called process and the number printed by shell is called process-id (PID), this PID can be use to refer specific running process.
|
||||||
|
|
||||||
|
ps - show current running
|
||||||
|
kill {PID} - kill process
|
||||||
|
kill 0 - stop all processes except shell
|
||||||
|
& - background process
|
||||||
|
ps aux - display the owner of the processes along with the processes
|
||||||
|
ps -ag - get information of all running processes
|
||||||
|
ps ax | grep processes-you-want-to-see
|
||||||
|
top - see currently running processes and other information like memory and CPU
|
||||||
|
pstree - display a tree of processes
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Useful Cmds:
|
||||||
|
------------
|
||||||
|
|
||||||
|
List size of folders
|
||||||
|
$ du -sh *
|
||||||
|
|
||||||
|
|
15
shell_scripts/lessons/arguments.ssh
Executable file
15
shell_scripts/lessons/arguments.ssh
Executable file
|
@ -0,0 +1,15 @@
|
||||||
|
#! /bin/bash
|
||||||
|
# script to test for loop and arguments
|
||||||
|
|
||||||
|
if [ $# -eq 0 ]
|
||||||
|
then
|
||||||
|
echo "Error - Number missing form command line argument"
|
||||||
|
echo "Syntax : $0 number"
|
||||||
|
echo "Use to print multiplication table for given number"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
n=$1
|
||||||
|
for i in 1 2 3 4 5 6 7 8 9 10
|
||||||
|
do
|
||||||
|
echo "$n * $i = `expr $i \* $n`"
|
||||||
|
done
|
18
shell_scripts/lessons/cheesboard.sh
Executable file
18
shell_scripts/lessons/cheesboard.sh
Executable file
|
@ -0,0 +1,18 @@
|
||||||
|
#! /bin/bash
|
||||||
|
# script to print a chessboard
|
||||||
|
|
||||||
|
for (( i = 1; i <= 9; i++ ))
|
||||||
|
do
|
||||||
|
for (( j = 1; j <= 9; j++))
|
||||||
|
do
|
||||||
|
tot=`expr $i + $j`
|
||||||
|
tmp=`expr $tot % 2`
|
||||||
|
if [ $tmp -eq 0 ]; then
|
||||||
|
echo -e -n "\033[47m "
|
||||||
|
else
|
||||||
|
echo -e -n "\033[40m "
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo -e -n "\033[40m" # set to black
|
||||||
|
echo"" # print new line
|
||||||
|
done
|
9
shell_scripts/lessons/echo_cmds.sh
Executable file
9
shell_scripts/lessons/echo_cmds.sh
Executable file
|
@ -0,0 +1,9 @@
|
||||||
|
#! /bin/bash
|
||||||
|
echo "The echo commands are (use echo -e):"
|
||||||
|
echo
|
||||||
|
echo -e "\a alert "
|
||||||
|
echo -e "\n new line"
|
||||||
|
echo -e "\t horizontal tab"
|
||||||
|
echo -e "\c suppress trailing line"
|
||||||
|
echo -e "\r carriage return"
|
||||||
|
echo -e "\b backspace"
|
70
shell_scripts/lessons/if_example.sh
Executable file
70
shell_scripts/lessons/if_example.sh
Executable file
|
@ -0,0 +1,70 @@
|
||||||
|
#! /bin/bash
|
||||||
|
#
|
||||||
|
#Syntax:
|
||||||
|
# if condition
|
||||||
|
# then
|
||||||
|
# if condition
|
||||||
|
# then
|
||||||
|
# .....
|
||||||
|
# ..
|
||||||
|
# do this
|
||||||
|
# else
|
||||||
|
# ....
|
||||||
|
# ..
|
||||||
|
# do this
|
||||||
|
# fi
|
||||||
|
# else
|
||||||
|
# ...
|
||||||
|
# .....
|
||||||
|
# do this
|
||||||
|
# fi
|
||||||
|
#
|
||||||
|
# You can also use
|
||||||
|
# elif
|
||||||
|
# ...
|
||||||
|
# then
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
echo "Here an example of how to use if condition for numbers:"
|
||||||
|
echo
|
||||||
|
echo "Type a number from 1 to 10:"
|
||||||
|
read number
|
||||||
|
if test $number -gt 5
|
||||||
|
then
|
||||||
|
echo "This number is larger than 5"
|
||||||
|
fi
|
||||||
|
if test $number -lt 5
|
||||||
|
then
|
||||||
|
echo "This number is smaller than 5"
|
||||||
|
fi
|
||||||
|
echo
|
||||||
|
echo "Operators are: -eq, -ne, -lt, -gt, -ge"
|
||||||
|
echo
|
||||||
|
echo "****************************************************"
|
||||||
|
echo
|
||||||
|
echo "Here an example of how to use if condition for strings:"
|
||||||
|
echo
|
||||||
|
echo "Type a number your name:"
|
||||||
|
read name
|
||||||
|
if test $name != "john"
|
||||||
|
then
|
||||||
|
echo "Your name is not John, its $name"
|
||||||
|
else
|
||||||
|
echo "Your name is john! What the odds?"
|
||||||
|
fi
|
||||||
|
echo
|
||||||
|
echo "Operators are: =, !=, -n (not null, exist), -z null (exist)"
|
||||||
|
echo
|
||||||
|
echo "****************************************************"
|
||||||
|
echo
|
||||||
|
echo "Here an example of how to use if condition for files:"
|
||||||
|
echo
|
||||||
|
echo "Operators are: -s (non empty), -f (exist), -d (directory exist), -w (writeable), -r (read-only), -x (executable)"
|
||||||
|
echo
|
||||||
|
echo "****************************************************"
|
||||||
|
echo
|
||||||
|
echo "Here an example of how to use logical operators:"
|
||||||
|
echo
|
||||||
|
echo "Operators are: ! (NOT), -a (AND), -o (OR)"
|
7
shell_scripts/lessons/inputs.sh
Executable file
7
shell_scripts/lessons/inputs.sh
Executable file
|
@ -0,0 +1,7 @@
|
||||||
|
#! /bin/bash
|
||||||
|
echo "We use read to read an input"
|
||||||
|
echo
|
||||||
|
echo "Type your name: "
|
||||||
|
read fname
|
||||||
|
echo "You name is $fname"
|
||||||
|
echo
|
15
shell_scripts/lessons/linux_console.sh
Executable file
15
shell_scripts/lessons/linux_console.sh
Executable file
|
@ -0,0 +1,15 @@
|
||||||
|
#! /bin/bash
|
||||||
|
echo "The Linux Console variables are:"
|
||||||
|
echo
|
||||||
|
echo "\33 is the escape char, to take an action. Ex: using \033[34m:"
|
||||||
|
echo -e "\033[34m Hello! \033[0m"
|
||||||
|
echo
|
||||||
|
echo " [ is the start, 34 the parameter, m is the letter (action)"
|
||||||
|
echo " letters can be h, l, m, q, s, u..."
|
||||||
|
echo " parameters can be 0, 1 (bold), 2 (dim intensity), 5 (blink), 7 (reverse), 11, 25, 27, 30-27 (colors), 4047 (bkgd colors)..."
|
||||||
|
echo
|
||||||
|
echo "For example:"
|
||||||
|
echo -e "\033[5m Hey! \033[0m"
|
||||||
|
echo
|
||||||
|
echo "Now, quotes... the black quote is used to execute a cmd (\`cmd\`)"
|
||||||
|
echo
|
12
shell_scripts/lessons/shell_variables.sh
Executable file
12
shell_scripts/lessons/shell_variables.sh
Executable file
|
@ -0,0 +1,12 @@
|
||||||
|
#! /bin/bash
|
||||||
|
echo "The variable of the system are:"
|
||||||
|
echo
|
||||||
|
echo "BASH_VERSION: " $BASH_VERSION
|
||||||
|
echo "LOGNAME: " $LOGNAME
|
||||||
|
echo "OSTYPE: " $OSTYPE
|
||||||
|
echo "PATH: " $PATH
|
||||||
|
echo "PWD: " $PWD
|
||||||
|
echo "USERNAME: " $USERNAME
|
||||||
|
echo
|
||||||
|
echo "Linux distribution, name, arch: $ uname -a"
|
||||||
|
echo
|
Loading…
Add table
Add a link
Reference in a new issue