Unix Scripting - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Unix Scripting

Description:

Act like mini-shell scripts. Can set ... Using the test Command ... the user to enter multiple entries (name, age, sex and favorite color) one at a time. ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 28
Provided by: til42
Learn more at: http://www.wildbill.org
Category:
Tags: mini | scripting | the | unix

less

Transcript and Presenter's Notes

Title: Unix Scripting


1
Unix Scripting
  • Some General Ramblings

2
Quoting
  • Single quote '...'
  • The shell ignores all enclosed characters
  • Double quote "..."
  • The shell ignores most enclosed characters
  • The shell does interpret , , and \
  • Backtick or Lefttick ...
  • Runs a command output treated as a variable
  • Backslash \
  • Special treatment for the following character.

3
Setting Variables
  • Setting variables
  • varvalue
  • ENVVARvalue
  • export ENVVAR
  • Note
  • variables dont affect parent shells
  • only environmental variables affect child shells

4
Using variables
  • - list of all command-line parameters. Bad.
  • _at_ Good list of all command-line parameters.
  • 1, 2, , 9 individual command line
    parameters

5
Shell Loops - if
  • if expr then cmd1 else cmd2 fi
  • cmd1 and cmd2 can be complex
  • the else can be omitted
  • if var yes
  • gt then
  • gt echo good
  • gt fi
  • good
  • if var yes then echo good fi

6
Shell Loops while, for
  • while expr
  • do
  • cmd
  • done
  • for var in a b c d e
  • do
  • cmd
  • done

7
Shell functions
  • Act like mini-shell scripts.
  • Can set variables in the current shell.
  • function-name ()
  • cmd1
  • cmd2

8
Using the test Command
  • The test command makes preliminary checks of the
    UNIX internal environment and other useful
    comparisons
  • Place the test command inside the shell script or
    execute it directly from the command line
  • The test command can be used to
  • Perform relational tests with integers
  • Test strings
  • Determine if a file exists and what type of file
    it is
  • Perform Boolean tests

9
Relational Integer Tests
The test command returns a value known as an exit
status, which is a numeric value and indicates
the results of the test performed true if 0
(zero) and false if 1 (one)
10
String Tests
11
Testing Files
12
Testing Files From The Prompt
touch testfile ls -l testfile -rw-r--r-- 1
student student 0 Oct 9 1145 testfile
test -x testfile echo ? 1 (Note 0 true and
1 false) chmod 700 testfile ls -l
testfile -rwxr-xr-x 1 student student
0 Oct 9 1145 testfile test -x testfile echo
? 0 test -f test_file echo ? 1
13
Performing Boolean Tests
AND returns true (0) if both expressions are
true, otherwise returns false (1) OR returns
true if either expression is true, otherwise if
neither is true, returns false ! negates the
value of the expression
14
Boolean Tests (testfile1 testfile2)
  • touch testfile1
  • test f testfile1 a x testfile2
  • Echo ?
  • touch testfile2
  • test f testfile1 a x testfile2
  • echo ?
  • chmod 777 testfile1
  • echo ?
  • test f testfile1 a x testfile2
  • rm testfile2
  • test f testfile1 a x testfile2

15
Script Exercise 1
  • Write a script that will echo to the screen at
    least three arguments that you include at the
    command line when you run the script

16
Script Exercise 1 Solution
  • !/bin/bash
  • echo 1 2 2

17
Script Exercise 2
  • Write a script that will create a file that is
    named when you execute the shell script
  • e.g.
  • myshell.sh testfile

18
Script Exercise 2 Solution
  • !/bin/bash
  • touch 1
  • or
  • echo testing gt 1
  • or
  • gt 1
  • or
  • ls gt 1

19
Script Exercise 3
  • Write a script that will create a file using
    todays date and the date format is ddmmmyy.dat

20
Script Exercise 3 Solution
  • !/bin/bash
  • touch date dby.dat
  • or.
  • FILENAMEdate dby.dat
  • Touch FILENAME
  • or etc.

21
Script Exercise 4
  • Write a script that will ask the user for to
    input a file name and then create the file and
    echo to the screen that the file name inputted
    had been created

22
Script Exercise 4 Solution
  • !/bin/bash
  • clear
  • echo enter a file name
  • read FILENAME
  • touch FILENAME
  • echo FILENAME has been created

23
Script Exercise 5
  • Now take the script from exercise 4 and write it
    so it will not create a file if no input is
    received

24
Script Exercise 5 Solution
  • !/bin/bash
  • clear
  • echo Enter a file name
  • read FILENAME
  • if ! z FILENAME then
  • touch FILENAME
  • echo FILENAME has been created
  • else
  • echo You did not enter a file name.
  • fi

25
Script Exercise 6
  • Write a script that asks for the users favorite
    color then if it is not red inform them that is
    the wrong answer, wait 3 seconds, clear the
    screen and ask the question again.

26
Script Exercise 7
  • Ask the user to enter multiple entries (name,
    age, sex and favorite color) one at a time. Then
    echo those answers back to the screen.

27
Script Exercise 8
  • Modify the script in exercise 7 to write the
    responses to a file named ddmmyy.dat one answer
    per line.
Write a Comment
User Comments (0)
About PowerShow.com