some sytem scripts

This commit is contained in:
Marina Wahl 2014-07-05 18:22:01 -04:00
parent 4b0a965e4a
commit 5852171b21
25 changed files with 639 additions and 1 deletions

9
bash_logout Normal file
View file

@ -0,0 +1,9 @@
echo
echo "Before leaving..."
echo
echo "Did you $ backup?"
echo
echo "Did you $ git everything?"
echo
echo "<3 Bye, $LOGNAME! <3"
echo

View file

@ -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

View 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"

View 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

View file

@ -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 "

View 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"

View 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
View file

View 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

View 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"

View 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.$$

View 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"

View 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"

View 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

View file

@ -0,0 +1,53 @@
Data Manipulation
-----------------
Print elements in the column 3 that has the word "good" in the other columns
$ awk '/good/ { print $3 }' inventory
Syntax:
$ awk -f {PROGRAM FILE} FILENAME
awk program is:
PATTERN{
ACTION 1
ACTION 2
ACTION 3
}
. (Dot) Match any character
* Match zero or more character
^ Match beginning of line
$ Match end of line
\ Escape character following
[ ] List
{ } Match range of instance
+ Match one more preceding
? Match zero or one preceding
| Separate choices to match
IF CONDITION
------------
if (condition)
{
Statement 1
Statement 2
Statement N
if condition is TRUE
}
else
{
Statement 1
Statement 2
Statement N
if condition is FALSE
}
FOR LOOP
--------

View file

@ -0,0 +1,41 @@
For files with many columns:
===========================
$ cut -f2 NAMEFILE --> display the second column
$ cut -f1 NAMEFILE --> display first column
$ paste FILE1 FILE2 --> join the columns together of two files
$ join FILE1 FILES2 --> join separate files but only if there is common field in both files with identical values
Changing the contend of files:
==============================
CHARACTERS:
----------
$ tr "h2" "3x" < FILENAME --> changes all h to 3 and all 2 to x
WORDS:
------
$ sed expression file
$ sed '/tea/s//milk/g' FILENAME
/tea/ --> find tea word
s//milk/ --> replace (substitute) the word milk for tea
g --> make the changes globally
DUPLICATES:
-----------
Print LINES that are unique.
$ uniq FILENAME
Duplicate lines must be next to next to each other, so it's better to sort first:
$ sort FILENAME | uniq
Ex
--
$ ex FILENAME --> start the editor

View file

@ -0,0 +1,21 @@
# awk -f FILEAWK FILELIST
BEGIN{
}
#
# main logic is here
#
{
sfile = $1
dfile = $2
cpcmd = "cp " $1 " " $2
printf "Coping %s to %s\n",sfile,dfile
system(cpcmd)
}
#
# End action, if any, e.g. clean ups
#
END{
}

View file

@ -0,0 +1,12 @@
# awk -f for_loop.awk
BEGIN {
printf "To test for loop\n"
printf "Press CTRL + C to stop\n"
}
{
for(i=0;i<NF;i++)
{
printf "Welcome %s, %d times.\n" ,ENVIRON["USER"], i
}
}

View file

@ -0,0 +1,8 @@
# awk -f math1
{
no1 = $1
no2 = $2
ans = $1 + $2
print no1 " + " no2 " = " ans
}

View file

@ -0,0 +1,30 @@
# awk -f math2
BEGIN {
myprompt = "(To Stop press CTRL+D) > "
printf "Welcome to MyAddtion calculation awk program v0.1\n"
printf "%s" ,myprompt
}
{
no1 = $1
op = $2
no2 = $3
ans = 0
if ( op == "+" )
{
ans = $1 + $3
printf "%d %c %d = %d\n" ,no1,op,no2,ans
printf "%s" ,myprompt
}
else
{
printf "Opps!Error I only know how to add.\nSyntax: number1 + number2\n"
printf "%s" ,myprompt
}
}
END {
printf "\nGoodbye, %s\n !" , ENVIRON["USER"]
}

9
shell_scripts/useful/bye.sh Executable file
View file

@ -0,0 +1,9 @@
echo
echo "Before leaving..."
echo
echo "Did you $ backup?"
echo
echo "Did you $ git everything?"
echo
echo "<3 Bye, $LOGNAME! <3"
echo

View file

@ -0,0 +1,15 @@
#!/bin/bash
if [ $# -ne 1 ]
then
echo "Usage - $0 file-name"
exit 1
fi
if [ -f $1 ]
then
echo "$1 file exist"
else
echo "Sorry, $1 file does not exist"
fi

30
shell_scripts/useful/menu.sh Executable file
View file

@ -0,0 +1,30 @@
#!/bin/sh
# Script to create simple menus and take action according to that selected
# menu item
#
while :
do
clear
echo "-------------------------------------"
echo " Main Menu "
echo "-------------------------------------"
echo "[1] Show Todays date/time"
echo "[2] Show files in current directory"
echo "[3] Show calendar"
echo "[4] Start editor to write letters"
echo "[5] Exit/Stop"
echo "======================="
echo -n "Enter your menu choice [1-5]: "
read yourch
case $yourch in
1) echo "Today is `date` , press a key. . ." ; read ;;
2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ; read ;;
3) cal ; echo "Press a key. . ." ; read ;;
4) vi ;;
5) exit 0 ;;
*) echo "Opps!!! Please select choice 1,2,3,4, or 5";
echo "Press a key. . ." ; read ;;
esac
done

View 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"

View file

@ -0,0 +1,61 @@
#!/bin/bash
nouser=`who | wc -l`
echo -e "User name: $USER (Login name: $LOGNAME)" >> /tmp/info.tmp.01.$$$
echo -e "Current Shell: $SHELL" >> /tmp/info.tmp.01.$$$
echo -e "Home Directory: $HOME" >> /tmp/info.tmp.01.$$$
echo -e "Your O/s Type: $OSTYPE" >> /tmp/info.tmp.01.$$$
echo -e "PATH: $PATH" >> /tmp/info.tmp.01.$$$
echo -e "Current directory: `pwd`" >> /tmp/info.tmp.01.$$$
echo -e "Currently Logged: $nouser user(s)" >> /tmp/info.tmp.01.$$$
if [ -f /etc/redhat-release ]
then
echo -e "OS: `cat /etc/redhat-release`" >> /tmp/info.tmp.01.$$$
fi
if [ -f /etc/shells ]
then
echo -e "Available Shells: " >> /tmp/info.tmp.01.$$$
echo -e "`cat /etc/shells`" >> /tmp/info.tmp.01.$$$
fi
if [ -f /etc/sysconfig/mouse ]
then
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "Computer Mouse Information: " >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "`cat /etc/sysconfig/mouse`" >> /tmp/info.tmp.01.$$$
fi
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "Computer CPU Information:" >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
cat /proc/cpuinfo >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "Computer Memory Information:" >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
cat /proc/meminfo >> /tmp/info.tmp.01.$$$
if [ -d /proc/ide/hda ]
then
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "Hard disk information:" >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "Model: `cat /proc/ide/hda/model` " >> /tmp/info.tmp.01.$$$
echo -e "Driver: `cat /proc/ide/hda/driver` " >> /tmp/info.tmp.01.$$$
echo -e "Cache size: `cat /proc/ide/hda/cache` " >> /tmp/info.tmp.01.$$$
fi
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "File System (Mount):" >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
cat /proc/mounts >> /tmp/info.tmp.01.$$$
if which dialog > /dev/null
then
dialog --backtitle "Linux Software Diagnostics (LSD) Shell Script Ver.1.0" --title "Press Up/Down Keys to move" --textbox /tmp/info.tmp.01.$$$ 21 70
else
cat /tmp/info.tmp.01.$$$ |more
fi
rm -f /tmp/info.tmp.01.$$$