Learn faster and stay on-track by joining this free class with other self-learners.
|
Bash ScriptingClass length: 18 weeks. Start anytime. Creator: kday Status: Established |
Join this class! |
|
Lesson 2: Assignment 1: Problem set 1Do all of the examples throughout Chapter 3 in Bash Cookbook by O'Reilly. To submit the homework, just leave a comment saying that you completed it and mention any problems you had or any cool things you learned. Don't print out your whole session history, it gets really long really fast. Do post code specific examples that you want to discuss. If you copy and paste any of your bash history, make sure that you don't post anything sensitive. Create dummy directories for working with files, don't post any passwords, etc. The assignment should take about 30 to 45 minutes to complete. Homework Submissions7 total/usr/bin/env shfunction choose { read -p "Would you give me your password? (y/n): " answer case $answer in [yY] ) echo "you are a darling" read -s -p "password: " password printf "n" ;; [nN] ) echo "You are angry with me" ;; * ) echo "Hmm...." ;; esac } choose if [ -n "$password" ]; then echo "Your password is:" $password fi I saved the errata for Bash Cookbook as html page and reference it for any typo's and such before working the chapter examples. Liked the bash builtin select No comments. Sign up or log in to comment Yikes. I see what everyone's saying about 3.6; that really is out of place. I see what they're trying to do, but the examples were so poor that the lesson is almost lost. For one, I'm not sure that they mentioned anywhere how the scripts even call the functions -- there's supposed to be something like what I've got below so you can source the functions files, otherwise the script has no idea what you're talking about. I'll show what I mean below. One other nitpick: the book makes a little typo - printf "%b" "Overriding $THISPACKAGEwith ${CHOICE}n" specifically $THISPACKAGEwith .. I'll mark it down in the code below. Maybe I had this all wrong, but that's how I saw it. Now, the "select" stuff, that was pretty awesome! #!/bin/bash
#source the function we want to use
source ~/func_choice.1
#also, the syntax '. ~/func_choice.1' (no quote marks, duh) works for that as well
THISPACKAGE=`date` # this wasn't included in the bash cookbook...?
until [ "$CHOICE" = "y" ]; do
printf "%b" "This package's date is $THISPACKAGE\n" >&2
choice "Is that correct? [Y/,<New date>]: "
if [ -z "$CHOICE" ]; then
CHOICE='y'
elif [ "$CHOICE" != "y" ]; then
# typo on the line below, should be "$THISPACKAGE with"
# below the dashes are the examples WITH the correct spacing,
# and without the spacing is the example right below it
printf "%b" "Overriding $THISPACKAGEwith ${CHOICE}\n"
THISPACKAGE=$CHOICE
fi
done
[root@atlas ~]#
-------------------
[root@atlas ~]# ./choice.sh
This package's date is Fri Nov 26 21:39:39 PST 2010
Is that correct? [Y/,<New date>]: 11/27/2010
Overriding 11/27/2010
This package's date is 11/27/2010
Is that correct? [Y/,<New date>]: y
[root@atlas ~]#
[root@atlas ~]# ./choice.sh
This package's date is Fri Nov 26 21:39:08 PST 2010
Is that correct? [Y/,<New date>]: 11/27/2010
Overriding Fri Nov 26 21:39:08 PST 2010 with 11/27/2010
This package's date is 11/27/2010
Is that correct? [Y/,<New date>]: y
No comments. Sign up or log in to comment
No comments. Sign up or log in to comment Pacing in this chapter is pretty much crap, in 3.6 the author really drops a load on the reader. But the select function is really cool! Comments:Cool, nice job. -- oops I posted my homework in your comment, sorry. No problem. Nice job on the homework. Glad to see others succeeding with this class. Complete. The select function in 3.7 is pretty cool. I didn't know that existed. I got the following error when I tried 3.6. Not sure how to fix it because they kind of just put a big script in there without much explanation. Otherwise the homework went well. # cookbook filename: func_choose
# Let the user make a choice about something and execute code based on
# the answer
# Called like: choose <default (y or n) > <prompt> <yes action> <no action>
# e. g. choose "y" \
# "Do you want to play a game?" \
# /usr/games/GlobalThermonucularWar \
# ' printf "%b" "See you later Professor Falkin. "' >&2
# Returns: nothing
function choose {
local default="$1"
local prompt="$2"
local choice_yes="$3"
local choice_no="$4"
local answer
read -p "$prompt" answer
[ -z "$answer" ] && answer="$default"
case "$answer" in
[ yY1] ) exec "$choice_yes"
# error check
;;
[ nN0] ) exec "$choice_no"
# error check
;;
* ) printf "%b" "Unexpected answer ' $answer' ! " >&2 ; ;
esac
} # end of function choose
choose y "choose one" {echo "yes"} {echo "no"}
$./func_choose
$./func_choose: line 19: syntax error near unexpected token `yY1]'
Comments:change your function call a bit: choose "y" "choose y or n: " 'echo "you choose y"' 'echo "you chose n"' or even more fun: choose "y" "Is this the coolest thing EVAR? " 'echo -e "nTHEBESTTHEBESTTHEBESTn"' ' printf "%b" "Cool story bro. n"' >&2 Sorry, lost some formatting. choose "y" "choose y or n: " 'echo "you choose y"' 'echo "you chose n"' choose "y" \ "Is this the coolest thing EVAR? " \ 'echo -e "\nTHEBESTTHEBESTTHEBEST\n"' \ ' printf "%b" "Cool story bro. \n"' >&2 Ahh, thanks, that helps. I wasn't using it correctly at all. And it's only right there in the comments that I reposted.. whoops. Thanks. i didn't get quite get $3.6 and will have to revisit it |
No comments. Sign up or log in to comment