Anyone know what Awe is? [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-9
down vote

favorite












I have an assignment due for class and can find nothing about 'awe'?



The assignment says to create a script with a function called 'awe' that echo's the words "Because Linux is Awesome!" after 3 questions.



I'm very new to all this and would really like the help. I tried awk but from what I have seen in the videos I watched about this that this is not what he wants. Thanks.







share|improve this question














closed as too broad by G-Man, Stephen Rauch, Jeff Schaller, agc, Fox Nov 21 '17 at 2:46


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 8




    The question tells you exactly what the instructor wants. Create a script. Add a function to the script. Call this function "awe". "Awe" does not exist, and will not exist, until you create it.
    – John
    Nov 20 '17 at 22:38






  • 1




    You don't need awk for asking questions, echo and read shell built ins will be enough for this assignment. Also, idownvotedbecau.se/noattempt
    – myaut
    Nov 20 '17 at 22:40






  • 5




    Marci, you don't just Google the solution and copy-paste it somewhere. You learn principles, and create the solution yourself, only that way will make your education worth something (when you can apply these principles for some real-world shell script).
    – myaut
    Nov 20 '17 at 22:43






  • 3




    How would you feel if you were in a bus or a taxicab, the traffic light turned red, and the driver pulled out a computer and Googled “What do I do when the traffic light turns red?”  You go to school to learn information, not to get passing grades on your homework by turning around and asking somebody else for all the answers.
    – G-Man
    Nov 20 '17 at 23:41






  • 3




    Although this should probably not actually be reopened in its current state, and the downvotes are reasonable, the reaction expressed in comments here is absurd. Some students unfortunately paste homework problems without applying effort, but that's not what has happened here. In this situation, the student was unaware that user-defined shell functions existed at all and assumed that there must be some existing facility called awe. Would you attack the professor for bad teaching, without knowing the details of how this misconception occurred? No? Then don't attack the student, either.
    – Eliah Kagan
    Nov 21 '17 at 8:04














up vote
-9
down vote

favorite












I have an assignment due for class and can find nothing about 'awe'?



The assignment says to create a script with a function called 'awe' that echo's the words "Because Linux is Awesome!" after 3 questions.



I'm very new to all this and would really like the help. I tried awk but from what I have seen in the videos I watched about this that this is not what he wants. Thanks.







share|improve this question














closed as too broad by G-Man, Stephen Rauch, Jeff Schaller, agc, Fox Nov 21 '17 at 2:46


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 8




    The question tells you exactly what the instructor wants. Create a script. Add a function to the script. Call this function "awe". "Awe" does not exist, and will not exist, until you create it.
    – John
    Nov 20 '17 at 22:38






  • 1




    You don't need awk for asking questions, echo and read shell built ins will be enough for this assignment. Also, idownvotedbecau.se/noattempt
    – myaut
    Nov 20 '17 at 22:40






  • 5




    Marci, you don't just Google the solution and copy-paste it somewhere. You learn principles, and create the solution yourself, only that way will make your education worth something (when you can apply these principles for some real-world shell script).
    – myaut
    Nov 20 '17 at 22:43






  • 3




    How would you feel if you were in a bus or a taxicab, the traffic light turned red, and the driver pulled out a computer and Googled “What do I do when the traffic light turns red?”  You go to school to learn information, not to get passing grades on your homework by turning around and asking somebody else for all the answers.
    – G-Man
    Nov 20 '17 at 23:41






  • 3




    Although this should probably not actually be reopened in its current state, and the downvotes are reasonable, the reaction expressed in comments here is absurd. Some students unfortunately paste homework problems without applying effort, but that's not what has happened here. In this situation, the student was unaware that user-defined shell functions existed at all and assumed that there must be some existing facility called awe. Would you attack the professor for bad teaching, without knowing the details of how this misconception occurred? No? Then don't attack the student, either.
    – Eliah Kagan
    Nov 21 '17 at 8:04












up vote
-9
down vote

favorite









up vote
-9
down vote

favorite











I have an assignment due for class and can find nothing about 'awe'?



The assignment says to create a script with a function called 'awe' that echo's the words "Because Linux is Awesome!" after 3 questions.



I'm very new to all this and would really like the help. I tried awk but from what I have seen in the videos I watched about this that this is not what he wants. Thanks.







share|improve this question














I have an assignment due for class and can find nothing about 'awe'?



The assignment says to create a script with a function called 'awe' that echo's the words "Because Linux is Awesome!" after 3 questions.



I'm very new to all this and would really like the help. I tried awk but from what I have seen in the videos I watched about this that this is not what he wants. Thanks.









share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '17 at 15:40









Videonauth

1,038718




1,038718










asked Nov 20 '17 at 22:35









Marci

1




1




closed as too broad by G-Man, Stephen Rauch, Jeff Schaller, agc, Fox Nov 21 '17 at 2:46


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as too broad by G-Man, Stephen Rauch, Jeff Schaller, agc, Fox Nov 21 '17 at 2:46


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 8




    The question tells you exactly what the instructor wants. Create a script. Add a function to the script. Call this function "awe". "Awe" does not exist, and will not exist, until you create it.
    – John
    Nov 20 '17 at 22:38






  • 1




    You don't need awk for asking questions, echo and read shell built ins will be enough for this assignment. Also, idownvotedbecau.se/noattempt
    – myaut
    Nov 20 '17 at 22:40






  • 5




    Marci, you don't just Google the solution and copy-paste it somewhere. You learn principles, and create the solution yourself, only that way will make your education worth something (when you can apply these principles for some real-world shell script).
    – myaut
    Nov 20 '17 at 22:43






  • 3




    How would you feel if you were in a bus or a taxicab, the traffic light turned red, and the driver pulled out a computer and Googled “What do I do when the traffic light turns red?”  You go to school to learn information, not to get passing grades on your homework by turning around and asking somebody else for all the answers.
    – G-Man
    Nov 20 '17 at 23:41






  • 3




    Although this should probably not actually be reopened in its current state, and the downvotes are reasonable, the reaction expressed in comments here is absurd. Some students unfortunately paste homework problems without applying effort, but that's not what has happened here. In this situation, the student was unaware that user-defined shell functions existed at all and assumed that there must be some existing facility called awe. Would you attack the professor for bad teaching, without knowing the details of how this misconception occurred? No? Then don't attack the student, either.
    – Eliah Kagan
    Nov 21 '17 at 8:04












  • 8




    The question tells you exactly what the instructor wants. Create a script. Add a function to the script. Call this function "awe". "Awe" does not exist, and will not exist, until you create it.
    – John
    Nov 20 '17 at 22:38






  • 1




    You don't need awk for asking questions, echo and read shell built ins will be enough for this assignment. Also, idownvotedbecau.se/noattempt
    – myaut
    Nov 20 '17 at 22:40






  • 5




    Marci, you don't just Google the solution and copy-paste it somewhere. You learn principles, and create the solution yourself, only that way will make your education worth something (when you can apply these principles for some real-world shell script).
    – myaut
    Nov 20 '17 at 22:43






  • 3




    How would you feel if you were in a bus or a taxicab, the traffic light turned red, and the driver pulled out a computer and Googled “What do I do when the traffic light turns red?”  You go to school to learn information, not to get passing grades on your homework by turning around and asking somebody else for all the answers.
    – G-Man
    Nov 20 '17 at 23:41






  • 3




    Although this should probably not actually be reopened in its current state, and the downvotes are reasonable, the reaction expressed in comments here is absurd. Some students unfortunately paste homework problems without applying effort, but that's not what has happened here. In this situation, the student was unaware that user-defined shell functions existed at all and assumed that there must be some existing facility called awe. Would you attack the professor for bad teaching, without knowing the details of how this misconception occurred? No? Then don't attack the student, either.
    – Eliah Kagan
    Nov 21 '17 at 8:04







8




8




The question tells you exactly what the instructor wants. Create a script. Add a function to the script. Call this function "awe". "Awe" does not exist, and will not exist, until you create it.
– John
Nov 20 '17 at 22:38




The question tells you exactly what the instructor wants. Create a script. Add a function to the script. Call this function "awe". "Awe" does not exist, and will not exist, until you create it.
– John
Nov 20 '17 at 22:38




1




1




You don't need awk for asking questions, echo and read shell built ins will be enough for this assignment. Also, idownvotedbecau.se/noattempt
– myaut
Nov 20 '17 at 22:40




You don't need awk for asking questions, echo and read shell built ins will be enough for this assignment. Also, idownvotedbecau.se/noattempt
– myaut
Nov 20 '17 at 22:40




5




5




Marci, you don't just Google the solution and copy-paste it somewhere. You learn principles, and create the solution yourself, only that way will make your education worth something (when you can apply these principles for some real-world shell script).
– myaut
Nov 20 '17 at 22:43




Marci, you don't just Google the solution and copy-paste it somewhere. You learn principles, and create the solution yourself, only that way will make your education worth something (when you can apply these principles for some real-world shell script).
– myaut
Nov 20 '17 at 22:43




3




3




How would you feel if you were in a bus or a taxicab, the traffic light turned red, and the driver pulled out a computer and Googled “What do I do when the traffic light turns red?”  You go to school to learn information, not to get passing grades on your homework by turning around and asking somebody else for all the answers.
– G-Man
Nov 20 '17 at 23:41




How would you feel if you were in a bus or a taxicab, the traffic light turned red, and the driver pulled out a computer and Googled “What do I do when the traffic light turns red?”  You go to school to learn information, not to get passing grades on your homework by turning around and asking somebody else for all the answers.
– G-Man
Nov 20 '17 at 23:41




3




3




Although this should probably not actually be reopened in its current state, and the downvotes are reasonable, the reaction expressed in comments here is absurd. Some students unfortunately paste homework problems without applying effort, but that's not what has happened here. In this situation, the student was unaware that user-defined shell functions existed at all and assumed that there must be some existing facility called awe. Would you attack the professor for bad teaching, without knowing the details of how this misconception occurred? No? Then don't attack the student, either.
– Eliah Kagan
Nov 21 '17 at 8:04




Although this should probably not actually be reopened in its current state, and the downvotes are reasonable, the reaction expressed in comments here is absurd. Some students unfortunately paste homework problems without applying effort, but that's not what has happened here. In this situation, the student was unaware that user-defined shell functions existed at all and assumed that there must be some existing facility called awe. Would you attack the professor for bad teaching, without knowing the details of how this misconception occurred? No? Then don't attack the student, either.
– Eliah Kagan
Nov 21 '17 at 8:04










1 Answer
1






active

oldest

votes

















up vote
9
down vote













Answer using the Bash shell:




The assignment says to create a script with a function called 'awe' that echo's the words "Because Linux is Awesome!" after 3 questions.




The assignment says to create a function, so let's see what the Bash manual 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 as what this function shall do (output "Because Linux is Awesome!"). To output that message, you can use echo. The echo command, which Bash provides as a shell built-in, is used to output to stdout (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 accept several option flags. Let's see what the bash manual tells us about 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 the user a question and get input for them. Those options are:





  • -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 prompt → 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 from acting as an escape character. -p shows the user a prompt. You can put both options after a single -, so you might write something like this:



read -rp "Question? " variable


The user's input has been stored in variable. No we want to use that input, by including it in a sentence that we output back to the user. To expand variable into the value it holds, you put a $ in front of it. This works even within double quotes.



echo "This is a reply to the user and it contains $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 whether "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.




The test-command I have used in my example below is [, which is a shell built-in which tests for conditional true or false. When you use [, you must pass a final argument ]. = tests variables for equality as strings.



So, here is the example script:



#!/bin/bash

# function 'awe' to output "Because Linux is Awesome!"
awe()
echo "Because Linux is Awesome!"


# asking three questions
read -rp "Enter your name!: " name
echo "Well, hello then $name!"
read -rp "How old are you?: " age
echo "Being $age makes you nearly as old as I am"
read -rp "Do you like Linux?: " likes_linux

# acting on if the last input is yes or something else in which case we assume no
if [ "$likes_linux" = "yes" ];then
awe
else
echo "Why don't you like Linux?"
fi





share|improve this answer


















  • 4




    I have to wonder if people are downvoting this answer simply for its existence here. The direct reference to the manual provides OP and other new users the means to answer questions on their own, which is good for the site. I think a reference to the man and/or apropos commands might also be useful, taking context into account. The intermittent examples, explanations of rationale, and summaries of the manual quotes make this even more useful for the learner than a simple RTFM. In my opinion, this is an excellent answer, and I would upvote twice if I could.
    – Fox
    Nov 21 '17 at 11:27

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
9
down vote













Answer using the Bash shell:




The assignment says to create a script with a function called 'awe' that echo's the words "Because Linux is Awesome!" after 3 questions.




The assignment says to create a function, so let's see what the Bash manual 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 as what this function shall do (output "Because Linux is Awesome!"). To output that message, you can use echo. The echo command, which Bash provides as a shell built-in, is used to output to stdout (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 accept several option flags. Let's see what the bash manual tells us about 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 the user a question and get input for them. Those options are:





  • -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 prompt → 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 from acting as an escape character. -p shows the user a prompt. You can put both options after a single -, so you might write something like this:



read -rp "Question? " variable


The user's input has been stored in variable. No we want to use that input, by including it in a sentence that we output back to the user. To expand variable into the value it holds, you put a $ in front of it. This works even within double quotes.



echo "This is a reply to the user and it contains $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 whether "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.




The test-command I have used in my example below is [, which is a shell built-in which tests for conditional true or false. When you use [, you must pass a final argument ]. = tests variables for equality as strings.



So, here is the example script:



#!/bin/bash

# function 'awe' to output "Because Linux is Awesome!"
awe()
echo "Because Linux is Awesome!"


# asking three questions
read -rp "Enter your name!: " name
echo "Well, hello then $name!"
read -rp "How old are you?: " age
echo "Being $age makes you nearly as old as I am"
read -rp "Do you like Linux?: " likes_linux

# acting on if the last input is yes or something else in which case we assume no
if [ "$likes_linux" = "yes" ];then
awe
else
echo "Why don't you like Linux?"
fi





share|improve this answer


















  • 4




    I have to wonder if people are downvoting this answer simply for its existence here. The direct reference to the manual provides OP and other new users the means to answer questions on their own, which is good for the site. I think a reference to the man and/or apropos commands might also be useful, taking context into account. The intermittent examples, explanations of rationale, and summaries of the manual quotes make this even more useful for the learner than a simple RTFM. In my opinion, this is an excellent answer, and I would upvote twice if I could.
    – Fox
    Nov 21 '17 at 11:27














up vote
9
down vote













Answer using the Bash shell:




The assignment says to create a script with a function called 'awe' that echo's the words "Because Linux is Awesome!" after 3 questions.




The assignment says to create a function, so let's see what the Bash manual 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 as what this function shall do (output "Because Linux is Awesome!"). To output that message, you can use echo. The echo command, which Bash provides as a shell built-in, is used to output to stdout (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 accept several option flags. Let's see what the bash manual tells us about 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 the user a question and get input for them. Those options are:





  • -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 prompt → 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 from acting as an escape character. -p shows the user a prompt. You can put both options after a single -, so you might write something like this:



read -rp "Question? " variable


The user's input has been stored in variable. No we want to use that input, by including it in a sentence that we output back to the user. To expand variable into the value it holds, you put a $ in front of it. This works even within double quotes.



echo "This is a reply to the user and it contains $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 whether "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.




The test-command I have used in my example below is [, which is a shell built-in which tests for conditional true or false. When you use [, you must pass a final argument ]. = tests variables for equality as strings.



So, here is the example script:



#!/bin/bash

# function 'awe' to output "Because Linux is Awesome!"
awe()
echo "Because Linux is Awesome!"


# asking three questions
read -rp "Enter your name!: " name
echo "Well, hello then $name!"
read -rp "How old are you?: " age
echo "Being $age makes you nearly as old as I am"
read -rp "Do you like Linux?: " likes_linux

# acting on if the last input is yes or something else in which case we assume no
if [ "$likes_linux" = "yes" ];then
awe
else
echo "Why don't you like Linux?"
fi





share|improve this answer


















  • 4




    I have to wonder if people are downvoting this answer simply for its existence here. The direct reference to the manual provides OP and other new users the means to answer questions on their own, which is good for the site. I think a reference to the man and/or apropos commands might also be useful, taking context into account. The intermittent examples, explanations of rationale, and summaries of the manual quotes make this even more useful for the learner than a simple RTFM. In my opinion, this is an excellent answer, and I would upvote twice if I could.
    – Fox
    Nov 21 '17 at 11:27












up vote
9
down vote










up vote
9
down vote









Answer using the Bash shell:




The assignment says to create a script with a function called 'awe' that echo's the words "Because Linux is Awesome!" after 3 questions.




The assignment says to create a function, so let's see what the Bash manual 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 as what this function shall do (output "Because Linux is Awesome!"). To output that message, you can use echo. The echo command, which Bash provides as a shell built-in, is used to output to stdout (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 accept several option flags. Let's see what the bash manual tells us about 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 the user a question and get input for them. Those options are:





  • -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 prompt → 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 from acting as an escape character. -p shows the user a prompt. You can put both options after a single -, so you might write something like this:



read -rp "Question? " variable


The user's input has been stored in variable. No we want to use that input, by including it in a sentence that we output back to the user. To expand variable into the value it holds, you put a $ in front of it. This works even within double quotes.



echo "This is a reply to the user and it contains $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 whether "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.




The test-command I have used in my example below is [, which is a shell built-in which tests for conditional true or false. When you use [, you must pass a final argument ]. = tests variables for equality as strings.



So, here is the example script:



#!/bin/bash

# function 'awe' to output "Because Linux is Awesome!"
awe()
echo "Because Linux is Awesome!"


# asking three questions
read -rp "Enter your name!: " name
echo "Well, hello then $name!"
read -rp "How old are you?: " age
echo "Being $age makes you nearly as old as I am"
read -rp "Do you like Linux?: " likes_linux

# acting on if the last input is yes or something else in which case we assume no
if [ "$likes_linux" = "yes" ];then
awe
else
echo "Why don't you like Linux?"
fi





share|improve this answer














Answer using the Bash shell:




The assignment says to create a script with a function called 'awe' that echo's the words "Because Linux is Awesome!" after 3 questions.




The assignment says to create a function, so let's see what the Bash manual 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 as what this function shall do (output "Because Linux is Awesome!"). To output that message, you can use echo. The echo command, which Bash provides as a shell built-in, is used to output to stdout (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 accept several option flags. Let's see what the bash manual tells us about 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 the user a question and get input for them. Those options are:





  • -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 prompt → 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 from acting as an escape character. -p shows the user a prompt. You can put both options after a single -, so you might write something like this:



read -rp "Question? " variable


The user's input has been stored in variable. No we want to use that input, by including it in a sentence that we output back to the user. To expand variable into the value it holds, you put a $ in front of it. This works even within double quotes.



echo "This is a reply to the user and it contains $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 whether "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.




The test-command I have used in my example below is [, which is a shell built-in which tests for conditional true or false. When you use [, you must pass a final argument ]. = tests variables for equality as strings.



So, here is the example script:



#!/bin/bash

# function 'awe' to output "Because Linux is Awesome!"
awe()
echo "Because Linux is Awesome!"


# asking three questions
read -rp "Enter your name!: " name
echo "Well, hello then $name!"
read -rp "How old are you?: " age
echo "Being $age makes you nearly as old as I am"
read -rp "Do you like Linux?: " likes_linux

# acting on if the last input is yes or something else in which case we assume no
if [ "$likes_linux" = "yes" ];then
awe
else
echo "Why don't you like Linux?"
fi






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '17 at 14:45

























answered Nov 20 '17 at 23:01









Videonauth

1,038718




1,038718







  • 4




    I have to wonder if people are downvoting this answer simply for its existence here. The direct reference to the manual provides OP and other new users the means to answer questions on their own, which is good for the site. I think a reference to the man and/or apropos commands might also be useful, taking context into account. The intermittent examples, explanations of rationale, and summaries of the manual quotes make this even more useful for the learner than a simple RTFM. In my opinion, this is an excellent answer, and I would upvote twice if I could.
    – Fox
    Nov 21 '17 at 11:27












  • 4




    I have to wonder if people are downvoting this answer simply for its existence here. The direct reference to the manual provides OP and other new users the means to answer questions on their own, which is good for the site. I think a reference to the man and/or apropos commands might also be useful, taking context into account. The intermittent examples, explanations of rationale, and summaries of the manual quotes make this even more useful for the learner than a simple RTFM. In my opinion, this is an excellent answer, and I would upvote twice if I could.
    – Fox
    Nov 21 '17 at 11:27







4




4




I have to wonder if people are downvoting this answer simply for its existence here. The direct reference to the manual provides OP and other new users the means to answer questions on their own, which is good for the site. I think a reference to the man and/or apropos commands might also be useful, taking context into account. The intermittent examples, explanations of rationale, and summaries of the manual quotes make this even more useful for the learner than a simple RTFM. In my opinion, this is an excellent answer, and I would upvote twice if I could.
– Fox
Nov 21 '17 at 11:27




I have to wonder if people are downvoting this answer simply for its existence here. The direct reference to the manual provides OP and other new users the means to answer questions on their own, which is good for the site. I think a reference to the man and/or apropos commands might also be useful, taking context into account. The intermittent examples, explanations of rationale, and summaries of the manual quotes make this even more useful for the learner than a simple RTFM. In my opinion, this is an excellent answer, and I would upvote twice if I could.
– Fox
Nov 21 '17 at 11:27


Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos