mirror of
https://github.com/autistic-symposium/shell-whiz-toolkit.git
synced 2025-06-28 08:27:06 -04:00
some sytem scripts
This commit is contained in:
parent
4b0a965e4a
commit
5852171b21
25 changed files with 639 additions and 1 deletions
|
@ -50,4 +50,48 @@ Useful Cmds:
|
|||
List size of folders
|
||||
$ du -sh *
|
||||
|
||||
Print calendar:
|
||||
$ cal
|
||||
|
||||
Writing and creating a file:
|
||||
$ cat > NEWFILE
|
||||
|
||||
--- write here ---
|
||||
^D
|
||||
|
||||
|
||||
De-bug Shell Script
|
||||
-------------------
|
||||
|
||||
Use -v and -x option:
|
||||
$ sh option SCRIPT
|
||||
$ bash option SCRIPT
|
||||
|
||||
|
||||
Local and Global Variables
|
||||
-------------------------
|
||||
|
||||
Whenever you type:
|
||||
$ /bin/bash
|
||||
You start a new shell where the previous variables will be forgotten (local variables). To make variables global, you can do:
|
||||
$ export VARIABLE-NAME
|
||||
Before changing shell.
|
||||
|
||||
|
||||
Conditional Execution
|
||||
---------------------
|
||||
|
||||
The control operators are && (AND) and || (OR).
|
||||
$ rm myf && echo "File is removed successfully" || echo "File is not removed"
|
||||
|
||||
|
||||
Functions
|
||||
---------
|
||||
|
||||
$ SayHello()
|
||||
{
|
||||
echo "Hello $LOGNAME, Have nice computing"
|
||||
return
|
||||
}
|
||||
To execute this SayHello() function just type it name as follows:
|
||||
$ SayHello
|
31
shell_scripts/lessons/average_number.sh
Executable file
31
shell_scripts/lessons/average_number.sh
Executable file
|
@ -0,0 +1,31 @@
|
|||
#!/bin/bash
|
||||
#
|
||||
|
||||
avg=0
|
||||
temp_total=0
|
||||
number_of_args=$#
|
||||
|
||||
#
|
||||
# First see the sufficient cmd args
|
||||
#
|
||||
if [ $# -lt 2 ] ; then
|
||||
echo -e "Opps! I need atleast 2 command line args\n"
|
||||
echo -e "Syntax: $0: number1 number2 ... numberN\n"
|
||||
echo -e "Example:$0 5 4\n\t$0 56 66 34"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
#
|
||||
# now calculate the average of numbers given on command line as cmd args
|
||||
#
|
||||
|
||||
for i in $*
|
||||
do
|
||||
|
||||
# addition of all the numbers on cmd args
|
||||
temp_total=`expr $temp_total + $i`
|
||||
|
||||
done
|
||||
|
||||
avg=`expr $temp_total / $number_of_args`
|
||||
echo "Average of all number is $avg"
|
18
shell_scripts/lessons/case_loop_example.sh
Executable file
18
shell_scripts/lessons/case_loop_example.sh
Executable file
|
@ -0,0 +1,18 @@
|
|||
#!/bin/sh
|
||||
|
||||
# script to test case statement
|
||||
|
||||
if [ -z $1 ]
|
||||
then
|
||||
r="*** unknown ***"
|
||||
elif [ -n $1 ]
|
||||
then
|
||||
r=$1
|
||||
fi
|
||||
|
||||
case $r in
|
||||
"car") echo "car rental";;
|
||||
"van") echo "van rental";;
|
||||
"jeep") echo "jeep rental";;
|
||||
*) echo "Sorry I have no $r !";;
|
||||
esac
|
|
@ -5,7 +5,7 @@ for (( i = 1; i <= 9; i++ ))
|
|||
do
|
||||
for (( j = 1; j <= 9; j++))
|
||||
do
|
||||
tot=`expr $i + $j`
|
||||
tot=`expr $i + $j` # notice that these tot must have no space to =
|
||||
tmp=`expr $tot % 2`
|
||||
if [ $tmp -eq 0 ]; then
|
||||
echo -e -n "\033[47m "
|
||||
|
|
18
shell_scripts/lessons/factorial.sh
Executable file
18
shell_scripts/lessons/factorial.sh
Executable file
|
@ -0,0 +1,18 @@
|
|||
#! /bin/bash
|
||||
|
||||
n=0
|
||||
on=0
|
||||
fact=1
|
||||
|
||||
echo -n "Enter number to find factorial : "
|
||||
read n
|
||||
|
||||
on=$n
|
||||
|
||||
while [ $n -ge 1 ]
|
||||
do
|
||||
fact=`expr $fact \* $n`
|
||||
n=`expr $n - 1`
|
||||
done
|
||||
|
||||
echo "Factorial for $on is $fact"
|
12
shell_scripts/lessons/file_descriptors.sh
Executable file
12
shell_scripts/lessons/file_descriptors.sh
Executable file
|
@ -0,0 +1,12 @@
|
|||
#!/bin/sh
|
||||
|
||||
# from > & destination
|
||||
|
||||
if [ $# -ne 2 ]
|
||||
then
|
||||
echo "Error!" 1>&2
|
||||
echo "Usae: $0 number1 number2"
|
||||
exit 1
|
||||
fi
|
||||
ans=`expr $1 + $2`
|
||||
echo "Sum = $ans"
|
0
shell_scripts/lessons/for_loop_example.sh
Normal file → Executable file
0
shell_scripts/lessons/for_loop_example.sh
Normal file → Executable file
59
shell_scripts/lessons/getopts.sh
Executable file
59
shell_scripts/lessons/getopts.sh
Executable file
|
@ -0,0 +1,59 @@
|
|||
#!/bin/sh
|
||||
|
||||
|
||||
|
||||
# Usage: ./geotopts.sh -n -a -s -w -d
|
||||
#
|
||||
#
|
||||
# help_ani() To print help
|
||||
#
|
||||
help_ani()
|
||||
{
|
||||
echo "Usage: $0 -n -a -s -w -d"
|
||||
echo "Options: These are optional argument"
|
||||
echo " -n name of animal"
|
||||
echo " -a age of animal"
|
||||
echo " -s sex of animal "
|
||||
echo " -w weight of animal"
|
||||
echo " -d demo values (if any of the above options are used "
|
||||
echo " their values are not taken)"
|
||||
exit 1
|
||||
}
|
||||
#
|
||||
#Start main procedure
|
||||
#
|
||||
#
|
||||
#Set default value for variable
|
||||
#
|
||||
isdef=0
|
||||
na=Moti
|
||||
age="2 Months" # may be 60 days, as U like it!
|
||||
sex=Male
|
||||
weight=3Kg
|
||||
#
|
||||
#if no argument
|
||||
#
|
||||
if [ $# -lt 1 ]; then
|
||||
help_ani
|
||||
fi
|
||||
while getopts n:a:s:w:d opt
|
||||
do
|
||||
case "$opt" in
|
||||
n) na="$OPTARG";;
|
||||
a) age="$OPTARG";;
|
||||
s) sex="$OPTARG";;
|
||||
w) weight="$OPTARG";;
|
||||
d) isdef=1;;
|
||||
\?) help_ani;;
|
||||
esac
|
||||
done
|
||||
if [ $isdef -eq 0 ]
|
||||
then
|
||||
echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (user define mode)"
|
||||
else
|
||||
na="Pluto Dog"
|
||||
age=3
|
||||
sex=Male
|
||||
weight=20kg
|
||||
echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (demo mode)"
|
||||
fi
|
10
shell_scripts/lessons/hex_converter_real_numbers.sh
Executable file
10
shell_scripts/lessons/hex_converter_real_numbers.sh
Executable file
|
@ -0,0 +1,10 @@
|
|||
#! /bin/bash
|
||||
|
||||
n=0
|
||||
hex=0
|
||||
echo "Decimal to hexadecimal converter:"
|
||||
echo -n "Enter number in decimal format : "
|
||||
read n
|
||||
hex=`echo "obase=16;ibase=10; $n" | bc`
|
||||
echo "$n is equivalent \"$hex\" in hexadecimal"
|
||||
|
49
shell_scripts/lessons/menu_fancy.sh
Executable file
49
shell_scripts/lessons/menu_fancy.sh
Executable file
|
@ -0,0 +1,49 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# dialog --title {title} --backtitle {backtitle} {Box options}
|
||||
# where Box options can be any one of following
|
||||
# --yesno {text} {height} {width}
|
||||
# --msgbox {text} {height} {width}
|
||||
# --infobox {text} {height} {width}
|
||||
# --inputbox {text} {height} {width} [{init}]
|
||||
# --textbox {file} {height} {width}
|
||||
# --menu {text} {height} {width} {menu} {height} {tag1} item1}...
|
||||
#
|
||||
#
|
||||
|
||||
dialog \
|
||||
--title "Hello $LOGNAME" \
|
||||
--backtitle "Linux Shell Script - Bytegirl Tutorial" \
|
||||
--msgbox "This is dialog box called infobox, which is used \
|
||||
to show some information on screen, Thanks to Savio Lam and\
|
||||
Stuart Herbert to give us this utility. Press any key. . . " \
|
||||
9 50 ; read
|
||||
|
||||
|
||||
|
||||
dialog --title "Alert : Delete File" --backtitle "Linux Shell Script\
|
||||
Tutorial" --yesno "\nDo you want to delete '/usr/letters/jobapplication'\
|
||||
file" 7 60
|
||||
sel=$?
|
||||
case $sel in
|
||||
0) echo "User select to delete file";;
|
||||
1) echo "User select not to delete file";;
|
||||
255) echo "Canceled by user by pressing [ESC] key";;
|
||||
esac
|
||||
|
||||
|
||||
|
||||
$ cat > dia4
|
||||
dialog --title "Inputbox - To take input from you" --backtitle "Linux Shell\
|
||||
Script Tutorial" --inputbox "Enter your name please" 8 60 2>/tmp/input.$$
|
||||
|
||||
sel=$?
|
||||
|
||||
na=`cat /tmp/input.$$`
|
||||
case $sel in
|
||||
0) echo "Hello $na" ;;
|
||||
1) echo "Cancel is Press" ;;
|
||||
255) echo "[ESCAPE] key pressed" ;;
|
||||
esac
|
||||
|
||||
rm -f /tmp/input.$$
|
26
shell_scripts/lessons/real_numbers.sh
Executable file
26
shell_scripts/lessons/real_numbers.sh
Executable file
|
@ -0,0 +1,26 @@
|
|||
#! /bin/bash
|
||||
|
||||
|
||||
a=0
|
||||
b=0
|
||||
number_of_args=$#
|
||||
|
||||
#
|
||||
# First see the sufficient cmd args
|
||||
#
|
||||
if [ $# -lt 2 ] ; then
|
||||
echo -e "Opps! I need atleast 2 command line args\n"
|
||||
echo -e "Syntax: $0: number1 number2 ... numberN\n"
|
||||
echo -e "Example:$0 5 4\n\t$0 56 66 34"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
#
|
||||
# now calculate the average of numbers given on command line as cmd args
|
||||
#
|
||||
|
||||
a=$1
|
||||
b=$2
|
||||
|
||||
c=`echo $a + $b | bc`
|
||||
echo "$a + $b = $c"
|
33
shell_scripts/lessons/red_hello_world.sh
Executable file
33
shell_scripts/lessons/red_hello_world.sh
Executable file
|
@ -0,0 +1,33 @@
|
|||
#!/bin/bash
|
||||
|
||||
|
||||
clear
|
||||
echo -e "\033[1m Hello World"
|
||||
# bold effect
|
||||
echo -e "\033[5m Blink"
|
||||
# blink effect
|
||||
echo -e "\033[0m Hello World"
|
||||
# back to noraml
|
||||
|
||||
echo -e "\033[31m Hello World"
|
||||
# Red color
|
||||
echo -e "\033[32m Hello World"
|
||||
# Green color
|
||||
echo -e "\033[33m Hello World"
|
||||
# See remaing on screen
|
||||
echo -e "\033[34m Hello World"
|
||||
echo -e "\033[35m Hello World"
|
||||
echo -e "\033[36m Hello World"
|
||||
|
||||
echo -e -n "\033[0m "
|
||||
# back to noraml
|
||||
|
||||
echo -e "\033[41m Hello World"
|
||||
echo -e "\033[42m Hello World"
|
||||
echo -e "\033[43m Hello World"
|
||||
echo -e "\033[44m Hello World"
|
||||
echo -e "\033[45m Hello World"
|
||||
echo -e "\033[46m Hello World"
|
||||
|
||||
|
||||
echo -e "\033[0m Hello World"
|
16
shell_scripts/lessons/while_loop_example.sh
Executable file
16
shell_scripts/lessons/while_loop_example.sh
Executable file
|
@ -0,0 +1,16 @@
|
|||
#!/bin/sh
|
||||
|
||||
# script to test while statement
|
||||
|
||||
if [ $# -eq 0 ] # note the spaces
|
||||
then
|
||||
echo "Error, missing argument!"
|
||||
exit 1
|
||||
fi
|
||||
n=$1
|
||||
i=1
|
||||
while [ $i -le 10 ]
|
||||
do
|
||||
echo "$n * $i = `expr $i \* $n`"
|
||||
i=`expr $i + 1`
|
||||
done
|
Loading…
Add table
Add a link
Reference in a new issue