Skip to content

Instantly share code, notes, and snippets.

@Videonauth
Last active November 21, 2017 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Videonauth/1b55328aa2c122d3e0890a38eb3c84af to your computer and use it in GitHub Desktop.
Save Videonauth/1b55328aa2c122d3e0890a38eb3c84af to your computer and use it in GitHub Desktop.
# Answer using the Bash shell:
> It says to create a script with a function called awe and echoes the words "Because Linux is Awesome!" after 3 questions.
The assignment asks to create a function, so let's see what the [bash manual][1] tells us about functions
> Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular" command. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed. Shell functions are executed in the current shell context; no new process is created to interpret them.
> Functions are declared using this syntax:
> name () compound-command [ redirections ]
> or
> function name [()] compound-command [ redirections ]
> This defines a shell function named name. The reserved word function is optional. If the function reserved word is supplied, the parentheses are optional. The body of the function is the compound command compound-command (see Compound Commands). That command is usually a list enclosed between { and }, but may be any compound command listed above, with one exception: If the function reserved word is used, but the parentheses are not supplied, the braces are required. compound-command is executed whenever name is specified as the name of a command.
In short, a function is a set of commands bundled under a given name you supply, which is the name of the function.
The assignment tells you how the function shall be named (awe) as well what this function shall do (output "Because Linux is Awesome!") for which you can use `echo`. The `echo` command, which Bash provides as a shell built-in, is used to output to [`stdout`][2] (console). Given this information you can write your function:
awe(){
echo "Because Linux is Awesome!"
}
Next in the assignment is to make it execute after having asked three questions. For this you can use the `read` command which can have multiple options flags. Lets see what the [bash manual][1] tells us about it:
> read
>
> read [-ers] [-a aname] [-d delim] [-i text] [-n nchars]
> [-N nchars] [-p prompt] [-t timeout] [-u fd] [name …]
>
>One line is read from the standard input, or from the file descriptor fd
supplied as an argument to the -u option, split into words as described above in Word Splitting, and the first word is assigned to the first name, the second word to the second name, and so on. If there are more words than names, the remaining words and their intervening delimiters are assigned to the last name. If there are fewer words read from the input stream than names, the remaining names are assigned empty values. The characters in the value of the IFS variable are used to split the line into words using the same rules the shell uses for expansion (described above in Word Splitting). The backslash character ‘\’ may be used to remove any special meaning for the next character read and for line continuation. If no names are supplied, the line read is assigned to the variable REPLY.
`read` accepts several options. In this case, two are most relevant, since you want to ask a question and get input for them, and this is
> - `-r` → If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation.
> - `-p` → Display prompt, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal.
While this is not the worst situation to forget `-r` you almost always want to include it to prevent `\` to act as an escape character so we use it. and `-p`lets us put a prompt. A potential line might look like this since we can concatenate the option flags.
read -rp "Question? " VARIABLE
Now we want to act on given input and there you can simply output the given information in a sentence, to make the VARIABLE expand you put a `$`in front, this works as well within double quotes.
echo "a replaying message containing $VARIABLE!"
Normally this is all you need to solve this assignment, in my example script I added a little twist, to use a `if` condition to act upon wether yes or no was answered for the last question. So again lets see what the manual tells us about `if` conditions:
> The syntax of the if command is:
>
> if test-commands; then
> consequent-commands;
> [elif more-test-commands; then
> more-consequents;]
> [else alternate-consequents;]
> fi
> The test-commands list is executed, and if its return status is zero, the consequent-commands list is executed. If test-commands returns a non-zero status, each elif list is executed in turn, and if its exit status is zero, the corresponding more-consequents is executed and the command completes. If ‘else alternate-consequents’ is present, and the final command in the final if or elif clause has a non-zero exit status, then alternate-consequents is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.
So, here is the example script:
<!-- language: bash -->
#!/bin/bash
# function 'awe' to output "Because Linux is Awesome!
awe(){
echo "Because Linux is Awesome!"
}
# asking three questions
read -rp "Enter your name!: " ONE
echo "Well, hello then $ONE!"
read -rp "How old are you?: " TWO
echo "Being $TWO makes you nearly as old as I am"
read -rp "Do you like Linux?: " THREE
# acting on if the last input is yes or something else in which case we assume no
if [ $THREE = "yes" ];then
awe
else
echo "Why don't you like Linux?"
fi
[1]: https://www.gnu.org/software/bash/manual/bash.html
[2]: https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment