bash shell se 2xa3
play

Bash shell SE 2XA3 Term I, 2020/21 Outline Shells Shell scripts - PowerPoint PPT Presentation

Bash shell SE 2XA3 Term I, 2020/21 Outline Shells Shell scripts Shell variables Command-line arguments For loops Conditionals Shell customization Environment variables Aliases Shells A Unix shell is a command-line interpreter


  1. Bash shell SE 2XA3 Term I, 2020/21

  2. Outline Shells Shell scripts Shell variables Command-line arguments For loops Conditionals Shell customization Environment variables Aliases

  3. Shells ◮ A Unix shell is a command-line interpreter ◮ Interface to Unix/Linux OS ◮ Bourne bash shell; default on many systems ◮ which bash , answer /bin/bash only searches the directories listed in the PATH variable ◮ Also C shell csh , tcsh , Korn shell ksh , ...

  4. Shell scripts ◮ Text file containing shell commands ◮ First line specifies the shell to be used ◮ #!/bin/bash bash shell ◮ #!/bin/sh -x calls bash in debug mode; prints each line ◮ #!/bin/csh C shell

  5. Shell variables ◮ name=value no spaces around = ◮ To access the value, use $ , e.g. $PATH ◮ Examples ◮ name=/usr/lib/orb echo $name will show /usr/lib/orb ◮ x=‘ls‘ echo $x will show all items in the current directory, as if you typed ls ‘ls‘ – see 3.5.4. command substitution in Bash manual in Help section.

  6. Command-line arguments ◮ $0 name of the script/command ◮ $1 first argument ◮ $2 second argument, ... and so on ◮ $# is the number of command line arguments Try the following script: #!/bin/bash echo "My � name � is � $0" echo "First � argument � is � $1" echo "Second � argument � is � $2" � indicates space (blank) "My name is $0" – see 3.1.2. quoting in Bash manual in Help section. Why ' My name is $0 ' would not work?

  7. For loops for i in list do # command(s) done # indicates comment, try the following script #!/bin/bash files=‘ls‘ for i in $files do echo "Echoing file name: " $i done

  8. For loops for i do # command(s) done default is the list of command-line arguments

  9. Conditionals if cmd if cmd then then # command(s) # command(s) fi else # command(s) fi ◮ cmd is any command or command sequence ◮ true is when it returns 0 ◮ false is when it returns � = 0

  10. Nested conditionals if cmd if cmd then then # command(s) # command(s) else elif cmd if cmd then then # command(s) # command(s) else else # command(s) # command(s) fi fi fi Both notations will produce the same action; they are equivalent.

  11. Tests ◮ test evaluates any conditional expression 0 if true, 1 if false ◮ same as [ args ] , args is an expression if [ args ] then # command(s) else # command(s) fi

  12. Tests expression true if str1 equals str2 str1 = str2 str1 does not equal str2 str1 != str2 file exists and is readable -r file file exists and is writable -w file is a directory -d file is a regular file -f file is a file of size > 0 -s file expr1 -a expr2 expr1 and expr2 are both true expr1 -o expr2 expr1 or expr2 is true ! expr1 expr1 is not true (logical negation)

  13. Numeric tests expression true if num1 -eq num2 num1 equals num2 num1 -ne num2 num1 does not equal num2 num1 is smaller than num2 num1 -lt num2 num1 is smaller or equal to num2 num1 -le num2 num1 is greater than num2 num1 -gt num2 num1 is greater or equal to num2 num1 -ge num2

  14. Examples ◮ #!/bin/bash if [ "$1" = "foo" ] then echo "First argument is foo" else echo "First argument is not foo " fi ◮ if [ -r file.txt ] if file.txt is readable ◮ if [ "$1" = "foo" -a -r file.txt ] if the first argument is foo and file.txt is readable

  15. Examples ◮ #!/bin/bash if [ $# -eq 2 ] then echo "Number of arguments is OK" elif [ $# -lt 2 ] then echo "Number of arguments is too small" else echo "Number of arguments is too big " fi

  16. Shell customization ◮ .bashrc executes when Unix starts a new Bash shell ◮ .bash_profile executes on login .bashrc runs first ◮ C shell ◮ .cshrc ◮ .login

  17. Environment variables ◮ PATH specifies where the shell searches for commands ◮ export defines a variable ◮ to add /usr/local/bin to the path export PATH=$PATH:/usr/local/bin ◮ to add current directory to the path export PATH=$PATH:. ◮ HOME home directory ◮ to see all environment variables, use printenv

  18. Aliases ◮ Stored in .bash_profile ◮ alias newname= ' command ' examples: ◮ alias rm= ' rm -i ' to prompt before removing a file ◮ alias cp= ' cp -i ' to prompt before copying a file

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend