OPS435W4
Assignment #1 is up and running, what we need to do , last couple of weeks if we get a birthday date you can get the month year date out to do calculations and that’s all the assignment is about, write a script any user can run your script in two different ways, you create a file called age.bash and the user can provide you with your data in two different ways, one they put it on the command line as an argument, age.bash 19850330, or they can do it this way age.bash and no arguments, so in the first instance they are providing the data from the command line and the second they are not, there for the script must check to see if the user provided you with the data/argument, if they didn’t provide you the data you ask for it command to use to ask
Echo “YOUR DATA HERE”
Special command you use to capture data from the key board and that command is the
READ variable
So the variable name what ever name you assign to it that data will be automatically assigned to that variable, couple of examples later,
…once you have the data, and this applies to every program you write when communicating with users anytime you get data from the user you never use that data immediately you must verify it, if you try to use incorrect data you script will fail, FIRST VARIFY the data, once you ascertain the data is correct then you start using it not before, so we will see how you do this, your script for instance in two stages 1 and 2 the first stage and the submissions dates are different, stage 1 2 weeks stage 2 4 weeks, stage 1 verification of the data, first the we see the final result that should be displayed on the screen, all this is for stage two…..LETS LOOK AT STAGE 1
There are 5 steps you must go through
Step 1
The date is number 1
Determine they gave the data or not, if they have then you have the data if they did not provide you with the data on the command line then it is captured in the variable now you got to verify the data,
Step 2
Look at the data how long is it….is it 8 characters…….${} -this construct
If the eight chars are not their you got to tell
Then determine if their all numbers
Then check if the date is valid
We have to see through the cal date if its valid
Display feb 1989
U tell them what you entered and tell them its an invalid date, if the date it valid then you go check the last item to future date, you can have a user that hasn’t been born yet, if it is correct you have verified the data coming then parse it and do it for stage two
Lets see methods on how to do this, stage 1 should be inside a loop, if its incorrect you have to go back to top and verify from the begging stage one should be inside a loop data provided should be correct to get outtake the loop, for you u don’t have to have it on a loop. For our submission it’s a one shop deal, how do you know if they entered data on the command line…well you don’t yet so this is how its down.
Lecture Schedule Slides:
#! shbang
# comment
Understanding parameters when you enter anything on the command line the shell captures everything that’s their, its captured, lets say we run are script and we have multiple arguments lets say
A b c d e ………… j k l
The moment you hit the return key the shell captures everything on the command line….special variables called predefined special variable…..what they are is this the command is age.bash it will automatically be assigned to a special variable called 0, remember variables and what chars you can use to assign names to
Variables = letters, underscore, numbers as long as their not in the first
If you want to see what is the name of the program what command will you use….
echo $0 it will automatically tell you the name of the programs, if you say
echo $1 the first arguments and so on
$10 and up don’t exist , some of the new shells can go beyond but not all
Beside $0-9 the rest of var
$@ expands into all the arguments ( abcde…..) (puts “ “)
$* same thing as all ( this doesn’t put “ “)
$# it will tell you how many arguments were entered in the command line
$? The exit status of the last command executed
$$ PID of the currect running shell
Ex: ls A? && echo banana
Goes into $? If it = 0 then its successful, and if its not 0 then its failed
$! PID of the last command executed in the background
These are the parameters need to know the love, and $0-9
The read command and how it works,
Read a
Sits and waits for it
Echo $a will show you what was entered
Read a b
Reads the first word then the last one captures the rest
You can use the read command for everything
Read –p print the text on the screen more then one word put it in quotes…..
Read –p “give me your birthday” a b d
Read –s silent….it doesn’t show on the screen you use for passwords
This is how you capture data from a user from the keyboard using the read command.
Readonly makes it so you can change the value of the variable………………….
Now the
If
then
else
the if statements executes the command what ever u place after the if statement, when it executes the command it goes to $? To see if the command is successful or not and if its successful it will go to the then or it will go to the else, fi like end if. Simplest version if
If
Then
Fi
One command under command , else is optional.
Vi ass1
With the test command you can do checking of variable,
Test value1 value2
test $# != 0
if and a read command to see if it equals to zero.
Or you can do bd=$1 which will give you what was read from the command line, after the if statement echo the variable, run the program or script and see what happens
#!/bin/bash < tells the program where its run from shbang
You can use the test command under two different ments
[ $# = 0 ] ( testing the number of arguments equalling to zero ) && echo you did not enter any arguments on the command line && exit
Since we have entered four it will go to the next command the
More arg display the contents of the script
When you run g a program the sub-shell will run your program, so when you did echo $$ in your script you showed the PID of the shell running your script.
Echo –e – interrupts backslash characters, the UNIX command uses \ to tell what to do the Linux version of echo does not recognize \ chars
Echo –n do not echo the new line character if you don’t wanna go to the next line this is linux, unix does it this way echo \c
-e linux command recognise the back slash characters then you can use \n to say enter a new line
While loop, simply how they work, just like the if statement if followed by command while followed by a command and the while statement will check the $? Of that command to see if its successful or not, and if it successful it will execute between the do and the done, and from the done it will go back to the while, if it is it will go through the loop again
Is 11 not equal to 44
[ $1 –ne 44 ]
SHIFT – how it works, will shift the arguments the variable $1 through $9 once to the left, what will happened $2 into $1 the contents of $1 goes into the garbage bin its gone, what happens if we did
Shift 2 ---- shift twice to the left,
Find the programs in public, programs are arg and args
Echo –n new line take out
Or construct with the braces try and use that one
BREAK
Command we already know the
Grep command use a number of different options,
Grep pattern data
We will look at regular expression do the same thing for data rather then file names , similar chars with meanings, the grep command is the one that interprets the regular expression, it’s the command itself that does the interpretation of regular expression, when your using a pattern use a ‘ ‘ if you don’t have a special char the ‘’ are doing nothing the ‘’ tell the shell don’t touch this pass it on the command to interpret, if you had a char in their this is not a special char.
Grep searching for a pattern in a database and when it finds it what does it do, it displays the whole line you found the match in
Cal 05 1887|grep F
Number of options with this command, how do you display only the line that has 2 as a full word and not part of a pattern?
Cal 05 1887|grep –w 1
-w word only
-i ignore case
-q quiet
-E
grep and xtended grep, it can use regular exp and also you xtended reg xpression, regular grep search for one patter, x multi patters, if you wanted to use xtended grep egrep or grep –E
-c count of number of lines matched
-v all lines that do not match the patte
No comments:
Post a Comment