Difference between revisions of "Sample Scripts"
Jump to navigation
Jump to search
Line 174: | Line 174: | ||
</pre> | </pre> | ||
== Count Files in Directory== | == Count Files in Directory== | ||
count=`ls-l *.sh | wc -l 2> /dev/null | count=`ls-l *.sh | wc -l 2> /dev/null` | ||
if [ $count -ge 3 ]; then | if [ $count -ge 3 ]; then | ||
echo "There are $count files in dirctory" | echo "There are $count files in dirctory" |
Revision as of 20:08, 20 May 2019
Script using if
#!/bin/bash echo -e "This program adds entries to a family database file . \n" echo -e "Would you like to add an entry to the family database file? \n" read ANSWER1 if [ $ANSWER1 = "y" -o $ANSWER1 = "Y" ] then echo -e "Please enter the name of the family member --> \c" read NAME echo -e "Please enter the family menber's relation to you (i.e. mother) -->\c" read RELATION echo -e "Please enter the family member's telephone number -->\c" read PHONE echo -e "$NAME\t$RELATION\t$PHONE">>database fi echo -e "Would you like to search an entry in the family databae file?\n" read ANSWER2 if [ $ANSWER2="y" -o $ANSWER2="Y" ] then echo -e "What word would you like to look for? -->\c" read WORD grep "$WORD" database fi
Script using CASE
#!/bin/bash while true do clear echo -e "What would you like to do? Add and entry (a) Search an entry (s) Quit (q) Enter your choice (a/s/q)-->\c" read ANSWER case $ANSWER in a|A) echo -e "Please enter the name of the family member -->\c" read NAME echo -e "Please enter the family member's relation to you (i.e. mother) -->\c" read RELATION echo -e "Please enter the family member's telephone number -->\c" read PHONE echo -e "$NAME\t$RELATION\t$PHONE" >>database ;; s|S) echo -e "What word would you like to look for?-->\c" read WORD grep "$WORD" database sleep 4 ;; q|Q) exit ;; *) echo "You must enter either the letter a or s" sleep 4 ;; esac done
While Loop
#!/bin/bash index=1 while [ $index -lt 6 ] do echo "hello ${index}" ((index++)) done
while [ "$correct" != "y" ] do read -p "enter your name:" name read -p "is ${name} matched" correct done
Files in folder to html
echo "print directory conents to file" echo -e "what is the title---->\c" read title sleep 1 echo -e '<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>' > index.html sleep 2 echo -e $title >> index.html sleep 2 echo '</title>' >> index.html sleep 2 echo -e '</head> <body>' >> index.html sleep 2 echo '<h1>' >> index.html sleep 2 echo $title >> index.html sleep 2 echo '</h1>' >> index.html sleep 2 ls | awk -F: '{print "<p> <a href=\""$1 "\">"$1 "</a></p>"}' >> index.html sleep 2 echo -e '</body></html' >> index.html
Reading a file, line by line
read -p "Enter Filename " FILENAME grep [rR]obert $FILENAME | while read LINE do echo "<p> ${LINE}</p>" done
Send Find and replace to a separate file=
read -p "Enter Filename " FILENAME sed 's/[rR]obert'xor/g' > two.txt sleep 2 #clear output file echo " Cleaned file " > three.txt grep -n -i xor two.txt | while read LINE do echo "<p> ${LINE} </p>" >> three.txt done clear echo "--------- two.txt -------------" cat two.txt echo "" echo "------three.txt ----------------" cat three.txt
Mysql backup to Amazon S3
#!/bin/bash clear mdate=`date +%F` echo "================ backing up databasename database ==============" mysqldump -u username -ppassword databasename > databasename-$mdate.sql sleep 5 echo "================ backing up databasename ====================" mysqldump -u username -ppassword databasename > databasename-$mdate.sql sleep 5 echo "=============== Backing up databasename database =============" mysqldump -u username -ppassword databasename > databasename-$mdate.sql sleep 5 echo " ========= done =========" ls *.sql > sqlfiles.txt clear cat sqlfiles.txt sleep 5 # calls another script which does the actual backup to amazon ./bksql.sh
Script that backs up to Amazon S3
#!/bin/bash echo "--------- backing up to s3 --------------" set -e if [ -f sqlfiles.txt ]; then while read line do aws s3 cp --region us-east-1 $line s3://bucketname done < sqlfiles.txt echo "++++++++++= Done =+++++++++++++++" else echo " XXXXXXXXXXX File Does not exist XXXXXXX" fi
Count Files in Directory
count=`ls-l *.sh | wc -l 2> /dev/null` if [ $count -ge 3 ]; then echo "There are $count files in dirctory" else echo "less than 3" fi