Shell Programming

Shell Programming

Shell Programming Erwin Earley ([email protected]) Sr. Professional Services Consultant @erwinephp @RougeWaveInc @Zend © 2018 Rogue Wave Software, Inc. All Rights Reserved. 1 Agenda • What is a Shell? • Working with the shell • Programming Constructs © 2018 Rogue Wave Software, Inc. All Rights Reserved. 2 What is a Shell? • The command line used on “Unix™” systems (as well as Unix-like systems) • Like CL it can be used interactively, or run as a Program • Like CL most commands are actually Programs that get called – There are some “built in” commands • Unlike CL there are a number of varieties of shell – sh bourne shell – csh c shell – ksh korn shell – bash bourne again shell – qsh Q shell • There are some similarities and some differences • Most of the discussion here is not oPerating system sPecific – Will worK on AIX, Linux, QSH in OS/400, other nasty Unix variants, etc © 2018 Rogue Wave Software, Inc. All Rights Reserved. 3 Why do we care about the shell? • All system configuration operations can be done through the shell – often more quickly then through a GUI • It may be the only environment available in the case of a system crash • Shell scripts can automate most routine tasks such as backups, scheduled emails, etc. • GUI can be used for a great amount of admin activities – However, the shell tends to be a comfort zone providing ability to fix things in case something goes wrong © 2018 Rogue Wave Software, Inc. All Rights Reserved. 4 Different Types of Shells • A number of shells are available each providing function/usability customized to a particular type of user: • Popular shells include: – BASH (Bourne Again Shell) – PDKsh (Public Domain Korn Shell) – csh (C shell) – mc (Midnight Commander) – QSHELL (PASE shell) – ksh (Korn shell, default on AIX) • Difference tends to be in scripting capabilities and user interface – Items such as command recall and file name completion are typically different © 2018 Rogue Wave Software, Inc. All Rights Reserved. 5 Login Shell vs. Non-Login Shell • Login Shell: Shell executed when user logs in to the system. – Typically performs general setup – initializes the terminal, sets some variables, etc. • Non-login shell: Either subshells (started from the login shell), shells started by the GUI desktop, or disconnected shells started by a command – Remember that shell is simply another command on the system • Login shells unlike Non-login shells read a series of configuration files: Login Shell: Non Login Shell: On login On startup if "/etc/profile”" source it if "~/.bashrc" exists, source it if "~/.bash_profile" exists, source it else if "~/.bash_login" exists, source it if "~/.profile" exists, source it On exit if "~/.bash_logout" exists, source it © 2018 Rogue Wave Software, Inc. All Rights Reserved. 6 Starting with bash • bash stands for Bourne Again Shell Available in PASE, more – Developed by Brian Fox in 1987 on that coming up! – One of the most popular shells available in Linux • Bash incorporates features of the Korn and C shell (ksh and csh) • Bash configuration files: /bin/bash – Bash executable /etc/profile – System wide initialization file for login shells ~/.bash_profile – Personal initialization file for login shells ~/.bashrc – Personal per-interactive-shell startup file ~/.bash_logout – Login shell clean file that executes when shell exits © 2018 Rogue Wave Software, Inc. All Rights Reserved. 7 A little more on BASH • Default Linux shell – This can be changed in a variety of ways • /etc/profile – login shell • $HOME/.profile • Magic string within the script (more on this in a moment) • Very powerful as a command line shell – Recall previous commands – Command and file completion with the <TAB> key • Many programming features – Loops and conditionals © 2018 Rogue Wave Software, Inc. All Rights Reserved. 8 Shell Environment • The shell is an environment where commands can be entered and the Operations system can respond to them • A key concept to the environment is environment variables – There are a large number of environment variables – HISTFILE: points to file containing the shell history, defaults to ~/.bash_history – HISTFILESIZE: how man last commands you wish to have in history – HOME: points to your home directory – PATH: set of directories to search when trying to execute a command – PS1: Prompt variable NOTE: All of these environment variables – USER: username are available when running bash in the PASE environment. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 9 Shell metacharacters Symbol Meaning > Output redirection >> Output redirection (append) < Input redirection * File substitution wildcard; zero or more characters ? File substitution wildcard; one character [ ] File substitution wildcard; any character between brackets `cmd` Command substitution $(cmd) Command substitution | The pipe (connect output of command on right to input on command on left) ; Command Sequence || OR conditional execution && AND conditional execution © 2018 Rogue Wave Software, Inc. All Rights Reserved. 10 Shell metacharacters (continued) Symbol Meaning ( ) Group commands & Run command in the background # Comment $ Expand the value of a variable \ Prevent or escape interpretation of the next character << Input redirection " $val " Literal with variable substitution ' $val ' Literal without variable substitution © 2018 Rogue Wave Software, Inc. All Rights Reserved. 11 Useful Shell Constructs • Arrow Up & Down: Scroll through recent commands used • &&: command is only executed if preceding command was successful: command1 && command2 • alias: sets a command alias or prints defined aliases alias wrklnk=ls • bg[jobid]: Resumes the suspended job in the background • cd: changes current directory to directory indicated cd /home © 2018 Rogue Wave Software, Inc. All Rights Reserved. 12 Useful Shell Constructs (continued) • echo: Outputs the arguments echo “hello world” • find [path][expression]: searches the directory indicating looking for files that match expression: find / -name passwd –print • pwd – Prints the absolute pathname of the current working directory • unalias – Removes an alias • history – displays command history with line numbers • umask – User file creation mask • logout: exits the shell environment • exit [n]: exits shell environment with exit status n © 2018 Rogue Wave Software, Inc. All Rights Reserved. 13 More information on Bash • Just enter the command – $ man Bash • Manual page for Bash is aBout 5,000 lines!! NOTE: the manual pages are not yet availaBle in the PASE environment. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 14 "bash"ing PASE – yes I went there ;-) • The bash shell is available for PASE – It is part of the RPM pile • Step 1: Install the RPM pile bootstrap • Step 2: Install bash yum install bash • Step 3: Create a home directory: PATH=/QOpenSys/pkgs/bin:$PATH mkdir /home/<username> export PATH • Step 4: Create a .profile • Step 5: Install nano bash yum install nano © 2018 Rogue Wave Software, Inc. All Rights Reserved. 15 qsh • A version of a shell for System i • Has many of the same features as bash • RTFM This is an excellent and comprehensive book! © 2018 Rogue Wave Software, Inc. All Rights Reserved. 16 Programming Constructs © 2018 Rogue Wave Software, Inc. All Rights Reserved. 17 The First Shell Program #!/bin/bash NOTE: on IBM i the path to bash is echo Hello World /QOpenSys/pkgs/bin/bash However, symbolic links have been created to /bin/bash and /usr/bin/bash This means that many existing scripts can be brought over from Linux environments and run unchanged in PASE under the bash shell • This is called a shell script • The first line "#!/bin/bash" is a magic string • When a file is loaded if the first two characters are “#!” (called shebang) then the remainder of the line is used as the program to run the file • "#!/home/erwin/my_nice_program" is a valid shell script • echo is a built-in command that Just displays the rest of the line © 2018 Rogue Wave Software, Inc. All Rights Reserved. 18 Creating a shell program • Use a text editor –Traditional Unix editors include vi and emacs –More user friendly editors include “nano” • On IBM i –Install and use nano from a PASE session © 2018 Rogue Wave Software, Inc. All Rights Reserved. 19 Writing Your Shell Script Using nano © 2018 Rogue Wave Software, Inc. All Rights Reserved. 20 Running Your Shell Script earley@testbed:~$ ls –l test.sh -rw-r—r– 1 earley earley 32 2006-09-11 08:54 test.sh earley@testbed:~$ test.sh -bash: test.sh: command not found earley@testbed:~$ • We have a "test.sh" program in our directory • Lets try and run it • Doesn’t work! Why not!?!? © 2018 Rogue Wave Software, Inc. All Rights Reserved. 21 Running Your Shell Script earley@testbed:~$ echo $PATH /home/earley/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/ usr/bin:/sbin:/bin:/usr/bin/X11 earley@testbed:~$ . test.sh Hello World earley@testbed:~$ • The program is not in the “path” – The current directory is not in the path • Can run a shell script by typing “.” – Means load the file into the current shell © 2018 Rogue Wave Software, Inc. All Rights Reserved. 22 Running Your Shell Script earley@testbed:~$ mv test.sh ~/bin/ earley@testbed:~$ echo $PATH /home/earley/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/ usr/bin:/sbin:/bin:/usr/bin/X11 earley@testbed:~$ test.sh -bash: /home/earley/bin/test.sh: Permission denied earley@testbed:~$ • Move the shell script into a directory in the path • Try and execute it again • Now we get “Permission denied”! Why!?!? © 2018 Rogue Wave Software, Inc. All Rights Reserved. 23 Running Your Shell Script earley@testbed:~$ ls –l ~/bin/test.sh -rw-r--r-- 1 earley earley 32 2006-09-11 08:54 /home/earley/bin/test.sh earley@testbed:~$ chmod u+x ~/bin/test.sh earley@testbed:~$ ls –l ~/bin/test.sh -rwx--r-- 1 earley earley 32 2006-09-11 08:54 /home/earley/bin/test.sh earley@testbed:~$ test.sh Hello World earley@testbed:~$ • The file is not marked with "executable" permissions • "chmod u+x …" says "make it executable by the user who owns it" • NOW it works © 2018 Rogue Wave Software, Inc.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    58 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us