Skip to content

Commit daf6bd9

Browse files
author
Gilberto Alvarado
committed
Checking in old svn stuff
1 parent eda7645 commit daf6bd9

20 files changed

+346
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
unixstuff
22
=========
33

4-
Unix stuff of course
4+
Compendium of unix exercises and useful utilities.
5+

advanced/password_checker

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
if [ $# = 0 ]; then
4+
echo "Usage: `basename $0` [password]"
5+
exit
6+
fi
7+
8+
password=$1
9+
length=`expr length $password`
10+
echo "Length: $length"
11+
if [ $length -le "8" ]; then
12+
echo "Password should be at least 8 characters"; exit 0
13+
elif [ ! `echo "$password" | grep -e [0-9]` ]; then
14+
echo "Password must contain at least one number"
15+
elif [ ! `echo $password | grep [\@\#\$\%\\*\\+\-\=]` ]; then
16+
echo "Password must contain at least one special character (@#$%$*+-=)"; exit 0
17+
else
18+
echo "Password is valid!"
19+
fi

basic/back_myself_up

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
#Script that backs itself up
4+
5+
cat `basename $0` > backup.sh
6+
7+

basic/directory_listing

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
#Script to list (recursively) any given directory contents and save them to a file
4+
5+
if [ $# != 2 ]; then
6+
echo "Usage: `basename $0` [directory] [backup_filename]"
7+
exit
8+
fi
9+
10+
dir=$1
11+
filename=$2
12+
ls -la $1 > $2

basic/karaoke

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
if [ $# = 0 ]; then
4+
echo "Usage: karaoke [filename]"; exit 1
5+
else
6+
file=$1
7+
if [ ! -f $file ]; then
8+
echo "File $file does not exist"; exit 0
9+
fi
10+
fi
11+
12+
clear
13+
echo -n
14+
i=1
15+
#Print bold text
16+
#\033 is an escape sequence, [1 turns on the bold attribute, [0 turns it off
17+
while read line; do
18+
echo -e "\033[${i}m$line\033[0m"
19+
sleep 1
20+
done<$file
21+

basic/options

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
#Script for getopts demonstration
4+
#
5+
# while getopts cdme OPTION; do
6+
# case ${OPTION} in
7+
# c) clear;;
8+
# d) ls -l ;;
9+
# m) echo "WTF is midnight commander?";;
10+
# e) vi ;;
11+
# *) echo "Invalid option"
12+
# esac
13+
# done
14+
15+
while getopts d:b:n:h OPTION; do
16+
case ${OPTION} in
17+
d) BACKUP_DIR=$OPTARG;;
18+
b) SLIDESHOWPRO_DIR=$OPTARG;;
19+
n) NUMBER_BACKUPS_PRESERVED=$OPTARG;;
20+
h) usage;;
21+
esac
22+
done
23+
echo $BACKUP_DIR
24+
echo $SLIDESHOWPRO_DIR
25+
echo $NUMBER_BACKUPS_PRESERVED

basic/q16_colorful

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
#Script for colorful prints
4+
#Color Foreground Background
5+
#black 30 40
6+
#red 31 41
7+
#green 32 42
8+
#yellow 33 43
9+
#blue 34 44
10+
#magenta 35 45
11+
#cyan 36 46
12+
#white 37 47
13+
14+
clear
15+
16+
echo -n
17+
#Print bold text
18+
#\033 is an escape sequence, [1 turns on the bold attribute, [0 turns it off
19+
echo -e "\033[1mThis is your life\033[0m"
20+
#Blink text
21+
echo -e "\033[5mBy The Killers\033[0m"
22+
#Underlined text
23+
echo -e "\033[4m Candid talks with strangers\033[0m"
24+
#Background color change. \E indicates background. First color is foreground
25+
#second color is background
26+
#tput sgr0 returns the terminal to its original state(you can also use \E[0m)
27+
echo -e '\E[34;47mBeauty lifes in danger'; tput sgr0
28+
echo -e '\E[1;33;44mNo one gives a damn about your hair'; tput sgr0
29+
30+
31+

basic/q4_calculator

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
#Basic calculator script
4+
#using case statement to perform basic math operation as follows:
5+
#+ addition
6+
#- subtraction
7+
#x multiplication
8+
#/ division
9+
# Usage example:
10+
#$ ./calculator 20 / 3,
11+
#Also checks for sufficient command line arguments
12+
13+
#first step: 3 and only 3 arguments
14+
if [ $# != 3 ]; then
15+
echo "3 arguments are required [operand][operator][operand]"
16+
else
17+
op1=$1
18+
op2=$3
19+
#Let's check operands
20+
case $2 in
21+
+) result=$(($op1 + $op2));;
22+
-) result=$(($op1 - $op2));;
23+
x) result=$(($op1 * $op2));;
24+
/) result=$(($op1 / $op2));;
25+
*) echo "Invalid operand";
26+
esac
27+
if [ ! -z $result ]; then
28+
echo $result
29+
fi
30+
fi
31+

basic/q6_reverse

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
#script to print given number in reverse order, for eg. If no is 123 it must print as 321.
4+
5+
#check for argument or add default
6+
if [ $# = 0 ]; then
7+
arg="backwards"
8+
else
9+
arg=$1
10+
fi
11+
#rev is a unix tool to reverse a string
12+
#This may fail if rev is in the wrong place
13+
result=`echo "$arg" | /usr/bin/rev`
14+
echo $result

basic/q7_sum_digits

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
#script to print given numbers sum of all digit,
4+
#For eg. If no is 123 it's sum of all digit will be 1+2+3 = 6.
5+
6+
if [ $# = 0 ]; then
7+
echo "Please provide a number"
8+
else
9+
sum=0
10+
number=$1
11+
#we have to know the length of the string
12+
#expr evaluates an expression and lets us manipulate strings
13+
length=`expr length $number`
14+
for((i=1; i<=$length; i++)); do
15+
digit=`expr substr $number $i 1`
16+
sum=$(($sum + $digit))
17+
done
18+
echo $sum
19+
fi

basic/sayhi

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
#Script for test (if)
3+
#Gets the time and salutes accordingly
4+
5+
FECHA=`date`;
6+
TIME=`date +%k`;
7+
if [ $TIME -le "6" ]; then
8+
MSG="Dude, you should be sleeping ";
9+
elif [ $TIME -le "12" ]; then
10+
MSG="Good morning ";
11+
elif [ $TIME -le "19" ]; then
12+
MSG="Good afternoon ";
13+
elif [ $TIME -le "24" ]; then
14+
MSG="Good night ";
15+
fi
16+
echo $MSG $1
17+
echo "today is: " $FECHA;
18+
echo "Users connected to the system: ";
19+
who

files/addnames

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
#Check number of arguments
4+
if [ $# != 2 ]; then
5+
echo "Exactly two arguments must be provided"
6+
exit;
7+
fi
8+
#Check filename
9+
if [ -f $1 ]; then
10+
echo "File $1 found"
11+
#Check if the name is already in the file
12+
name=`grep "$2" $1`
13+
if [ -n "$name" ]; then
14+
#Name is already in the file
15+
echo "Name found: $name"
16+
else
17+
# Concatenate name to current file
18+
$2 >> $1
19+
fi
20+
else
21+
echo "File $1 does not exist"
22+
exit;
23+
fi
24+

files/backup_files

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
#Backup files script
4+
#It will look for all the files with a given extension, remove dashes and spaces and back them up with a .bak extension.
5+
BACKUP_DIR="$HOME/backup"
6+
7+
if [ $# = 0 ]; then
8+
ext="*"
9+
else
10+
ext="*.$1"
11+
fi
12+
13+
if [ ! -d $BACKUP_DIR ]; then
14+
mkdir $BACKUP_DIR
15+
fi
16+
17+
for file in $ext; do
18+
if [ -d $file ]; then
19+
r="-r"
20+
fi
21+
cp $r "$file" "$BACKUP_DIR/`echo $file | sed -e 's| ||g' -e 's|-||'`"
22+
r=""
23+
# echo "$BACKUP_DIR/$file"
24+
done

files/classlist.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Oscar Gonzalez Huicochea
2+
Roberto Carrera Maldonado
3+
Daniel Valencia Backhoff
4+
Raul Alberto Huerta Barajas
5+
Raymundo Torres Bernal
6+
Nikita Belenkyi
7+
Jorge Galindo Robles
8+
Gabriela Huerta De la Sota Valek
9+
Sandra De la Torre Avila
10+
Francisco Carrillo Dominguez

files/create_dirs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
#Check number of arguments
4+
if [ $# = 0 ]; then
5+
echo "Please provide filename as an argument"
6+
exit
7+
fi
8+
9+
if [ -f $1 ]; then
10+
#File exits, let's read it and create some directories
11+
#First, parent directory
12+
if [ ! -d "submit" ]; then
13+
`mkdir submit`
14+
fi
15+
while read line; do
16+
#Check that directory does not exist yet
17+
if [ ! -d "./submit/$line" ]; then
18+
`mkdir "./submit/$line"`
19+
fi
20+
done < $1
21+
else
22+
#File does not exist.
23+
echo "File $1 does not exist"
24+
exit
25+
fi

files/create_test_files

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
#Script to create test files
4+
#An extension can be added as an argument. Default extension will be .txt
5+
6+
#Check for optional extension
7+
DIR="test_files"
8+
if [ $# = 0 ]; then
9+
ext="txt"
10+
else
11+
ext="$1"
12+
fi
13+
14+
if [ ! -d $DIR ]; then
15+
mkdir $DIR
16+
fi
17+
18+
# Create files
19+
for i in {0..5}; do
20+
touch "./$DIR/$i. - file.$ext"
21+
done

files/list_files

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
echo "Files in this directory that match *~ :"
4+
for file in *~; do
5+
echo $file
6+
done
7+

loops/countdown

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
#Despliega los numeros del 1 al 20
3+
var=20
4+
while [ $var -ge 0 ]; do
5+
echo $var
6+
let "var -= 1"
7+
done
8+
echo "GO!"

loops/countdown2

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
#Displays the numbers from param to 1
3+
4+
echo "Enter a number to start counting: "
5+
#Read variable from command line
6+
read var
7+
#Check if variable is not null (-z)
8+
if [ -z $var ]; then
9+
echo "No number detected. Defaut value is 20"
10+
#assign 20 to var as default value
11+
let "var = 20"
12+
fi
13+
14+
while [ $var -ge 0 ]; do
15+
echo $var
16+
let "var -= 1"
17+
done
18+
echo "GO!"
19+

loops/quoting

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
echo "WTF Script"
4+
list="yo tu el"
5+
for a in $list; do
6+
echo "El es $a"
7+
done
8+

0 commit comments

Comments
 (0)