unix scripting…

Download Unix Scripting…

Post on 30-Dec-2015

22 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Unix Scripting. Some General Ramblings. 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: ` ... ` - PowerPoint PPT Presentation

TRANSCRIPT

  • Unix ScriptingSome General Ramblings

  • QuotingSingle quote: '...'The shell ignores all enclosed charactersDouble quote: "..."The shell ignores most enclosed charactersThe shell does interpret $, `, and \Backtick or Lefttick: `...`Runs a command output treated as a variableBackslash: \Special treatment for the following character.

  • Setting VariablesSetting variablesvar=valueENVVAR=valueexport ENVVAR

    Note:variables dont affect parent shellsonly environmental variables affect child shells

  • Using variables$* - list of all command-line parameters. Bad.$@ Good list of all command-line parameters.$1, $2, , $9 individual command line parameters

  • Shell Loops - ifif expr ; then cmd1 ; else cmd2 ; ficmd1 and cmd2 can be complexthe else can be omitted$ if [ x"$var" = x"yes" ]> then> echo good> figoodif [ x$var = xyes ] ; then echo good ; fi

  • Shell Loops while, forwhile expr do cmd done

    for var in a b c d e do cmd done

  • Shell functionsAct like mini-shell scripts.Can set variables in the current shell.

    function-name () { cmd1cmd2}

  • Using the test CommandThe test command makes preliminary checks of the UNIX internal environment and other useful comparisonsPlace the test command inside the shell script or execute it directly from the command lineThe test command can be used to:Perform relational tests with integersTest stringsDetermine if a file exists and what type of file it isPerform Boolean tests

  • Relational Integer TestsThe 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)

  • String Tests

  • Testing Files

  • Testing Files From The Prompt$ touch testfile$ ls -l testfile-rw-r--r-- 1 student student 0 Oct 9 11:45 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 11:45 testfile$ test -x testfile$ echo $?0$ $ test -f test_file$ echo $?1

  • Performing Boolean TestsAND 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

  • Boolean Tests (testfile1 & testfile2)touch testfile1test f testfile1 a x testfile2Echo $?touch testfile2test f testfile1 a x testfile2echo $?chmod 777 testfile1echo $?test f testfile1 a x testfile2rm testfile2test f testfile1 a x testfile2

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

  • Script Exercise 1 Solution#!/bin/bashecho $1 $2 $2

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

  • Script Exercise 2 Solution#!/bin/bashtouch $1# orecho testing > $1# or> $1# orls > $1

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

  • Script Exercise 3 Solution#!/bin/bashtouch `date +%d%b%y`.dat# or.FILENAME=`date +%d%b%y`.datTouch $FILENAME# or etc.

  • Script Exercise 4Write 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

  • Script Exercise 4 Solution#!/bin/bashclearecho enter a file name: read FILENAMEtouch $FILENAMEecho $FILENAME has been created

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

  • Script Exercise 5 Solution#!/bin/bashclearecho Enter a file name: read FILENAMEif [ ! z $FILENAME ] ; thentouch $FILENAMEecho $FILENAME has been createdelseecho You did not enter a file name.fi

  • Script Exercise 6Write 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.

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

  • Script Exercise 8Modify the script in exercise 7 to write the responses to a file named ddmmyy.dat one answer per line.

Recommended

View more >