+ All Categories
Home > Documents > Summary (and not only) 3 - Decision making com… · Web viewSeminar #3 Summary (and not only)...

Summary (and not only) 3 - Decision making com… · Web viewSeminar #3 Summary (and not only)...

Date post: 28-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
3
Seminar #3 Summary (and not only) Decision making commands in bash: if and case During the seminar #3 I’ve presented to you some examples involving decision making (the commands if…fi and case…esac). if..fi command In the figure above you have a script example where we input two numbers stored in variables a and b and we compare (numerically) these two numbers using the if command, the condition being [ $a –gt $b ] (-gt stands for “greater or equal”). For more about different types of operators, see Chapter 15 in the unix_tutorial pdf. case..esac command
Transcript
Page 1: Summary (and not only) 3 - Decision making com… · Web viewSeminar #3 Summary (and not only) Decision making commands in bash: if and case During the seminar #3 I’ve presented

Seminar #3Summary (and not only)

Decision making commands in bash: if and case

During the seminar #3 I’ve presented to you some examples involving decision making (the commands if…fi and case…esac).if..fi command

In the figure above you have a script example where we input two numbers stored in variables a and b and we compare (numerically) these two numbers using the if command, the condition being [ $a –gt $b ] (-gt stands for “greater or equal”). For more about different types of operators, see Chapter 15 in the unix_tutorial pdf.case..esac command

Page 2: Summary (and not only) 3 - Decision making com… · Web viewSeminar #3 Summary (and not only) Decision making commands in bash: if and case During the seminar #3 I’ve presented

In the previous figure we have a simple case example: we enter the name of a colour, and depending of what we are entering, we get the corresponding message. If no “pattern” is recognized, we will get the last message: “I know nothing about it!!!!” (Remember that the * meta-character acts for any string of chars).NOTE. Redirecting standard outputAs we saw in previous examples at the course and seminar, we may redirect a command using the redirect output symbol (>). The format of the command line that redirects output is:command [arguments] > filenamewhere command is any executable program, arguments are optional arguments and filename is the name of the ordinary file the shell redirects the output to.We must be careful when redirecting output, because this operation may destroy a file! When we want to be prevented of overwriting files, we may use the noclobber function of the shell that prevents overwriting a file using redirection.Here is an example of how it’s working:

Page 3: Summary (and not only) 3 - Decision making com… · Web viewSeminar #3 Summary (and not only) Decision making commands in bash: if and case During the seminar #3 I’ve presented

So, using bash you may enable this feature by setting noclobber using the command set –o noclobber. We may reverse it using set +o noclobber.


Recommended