1 / 44

INFO 320 Server Technology I

INFO 320 Server Technology I. Week 5 Shell environments and scripting. Overview. Time for more practical concerns about how Linux/UNIX works Shells Aliases and links Compound commands Environment variables Scripting. Shells. From (Frisch, 2002), (Petersen,2009), (Nemeth, 2007). Shells.

Download Presentation

INFO 320 Server Technology I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INFO 320Server Technology I Week 5 Shell environments and scripting INFO 320 week 5

  2. Overview Time for more practical concerns about how Linux/UNIX works Shells Aliases and links Compound commands Environment variables Scripting INFO 320 week 5

  3. Shells From (Frisch, 2002), (Petersen,2009), (Nemeth, 2007) INFO 320 week 5

  4. Shells A shell is a text command line interface (CLI) environment Like ‘cmd’ in Windows or the Terminal utility in Mac OS X Ubuntu Server logs you into a bash shell, instead of using the GNOME GUI Each type of shell has its quirks For example, bash has a prompt of $ or #, but the C shell has a prompt of % INFO 320 week 5

  5. Shells Linux has several kinds of shells Korn shell (ksh is the command) Bourne shell (sh or dash, the original UNIX shell) Bourne Again SHell (bash, default in Ubuntu) Z shell (zsh) C shell (csh) TCSH shell (tcsh) INFO 320 week 5

  6. Shells In Linux sh points to bash csh points to tcsh In the GNOME GUI, we’re using a shell emulator Key shell files (no games, just files) /etc/shells lists the shells available /etc/passwd can specify the default shell INFO 320 week 5

  7. chsh chsh is a command to change shells It’s often used with the –s option, which is followed by the shell path & command The shell used must appear in /etc/shells Most shell commands are in /bin/ How else could you find them? So typical use of chsh is chsh –s /bin/sh INFO 320 week 5

  8. Aliases and links INFO 320 week 5

  9. Aliases We can define a shortcut for a command in the form of an alias alias alias-name=string If I hate typing ls –al all the time I could define alias z=“ls –al” Aliases only apply to the current shell, so they are often defined in the personal initialization files ~/.bash_profile, ~/.bash_login, or ~/.profile INFO 320 week 5

  10. Unalias The alias command by itself gives a list of all current aliases alias To remove an alias, use unalias unalias alias-name For example unalias z INFO 320 week 5

  11. Links Linux recognizes two forms of links Symbolic and hard Symbolic links are a pointer to the real file ln –s today weather creates a symbolic link weather which points to the file today Hard links are another name for the file Delete a hard link and you delete the real file ln Monday storm creates a hard link between storm and Monday INFO 320 week 5

  12. Links In ls –l, symbolic links show up with an ‘l’ (ell) in the first column, are very small If you erase the real file, symbolic links still exist, and have to be deleted separately Similarly, to erase a file with hard links, the real file and all hard links must be deleted A Windows Shortcut is a symbolic link, as is the OS X Alias INFO 320 week 5

  13. Compound commands From the CommandlineHowto, (Petersen,2009) INFO 320 week 5

  14. Background processes Shell commands are normally interactive You give a command, and have to wait for it to finish before giving the next command We can override this by running commands in the background with an & after the command tail –f psout & Can use ps –ef to tell when they’re done INFO 320 week 5

  15. Executing multiple commands If you want to execute two or more commandsconsecutively, then you can use a semicolon between them command1 ; command2 Spaces around the semicolon are optional Like this ps -ef;ls INFO 320 week 5

  16. Executing multiple commands It’s possible to make later commands dependent on the earlier commands executing successfully, by using && between the commands command1 && command2 Notice the && is a logical AND If command1 runs, then run command2 INFO 320 week 5

  17. Executing multiple commands Or if we want a command to execute only if the first onefails, use a logical OR which is || (two vertical lines) command1 || command2 Execute command2 only if command1 fails INFO 320 week 5

  18. Pipes Be careful! A single vertical line between commands is a pipe, which takes the output from one command and uses it as input for the next command command1 | command2 For example, the morecommand forces display of one page of text at a time So ls –al | more does what? INFO 320 week 5

  19. Pipes and grep Pipes are great for use with grep By itself, grep string filesearches for a string within the file grep booker facultylisting.txt Using pipes, you can search the output of a command with grep ls –al | grep apache ls –al | grep apache > apachefiles INFO 320 week 5

  20. Pipes into sorting, head or tail Pipes can feed sort, head, or tail sortdoes what it sounds like headgives the first ten lines of the file tailgives the last ten lines of the file Great for looking at the end of a log file, for example INFO 320 week 5

  21. Pipes and disk usage The disk usage command, du gives the space used by each directory This is a prime candidate for sorting in reverse order du / | sort –rn | head du –s /home/mary gives a summary df gives file system usage INFO 320 week 5

  22. Output redirection We saw that the default output can be redirected to a file with the > sign command > filename If you want to add (append) to the end of an existing file, then you would use the following syntax: command >> filename INFO 320 week 5

  23. Input redirection We can also feed a file into a command using input redirection We are going to take the input from a file for the command to be executed Here is the syntax for this input redirection command < filename sort < myfavoriteanimals.txt INFO 320 week 5

  24. Other wildcards We saw that * can represent any number of characters, like info3*.txt If you want a one character wildcard use ? ls –al file0?.txt If only a specific range of single characters are allowed, put them in square brackets ls –al file0[5-8].txt ls –al file[a-zA-Z].txt ls –al file[2589].txt INFO 320 week 5

  25. touch The touchcommand is an oddity “The touch utility shall change the modification times, access times, or both of files.” But if you touch a file that doesn’t exist, a blank file will be created So we’ll use touch to create a new file INFO 320 week 5

  26. Environment variables INFO 320 week 5

  27. Environment variables Environment variables are maintained by the OS to keep track of data Some pertain to a login session, some to a shell, some to other things Environment variables are in ALL CAPS, and to refer to them put a dollar sign $ before their name They have an underline between_words INFO 320 week 5

  28. Environment variables For example, your default shell is $SHELL To view the current value of an environment variable the echo command can be used echo $SHELL There are also environment variables within scripting, which depend on the type of shell you’re using INFO 320 week 5

  29. Environment variables There are lots of environment variables $HOME = home directory cd ~ returns you to your $HOME directory $TERM = the type of terminal mode $PWD = same as the command pwd $PS1 = the shell prompt To see a list of them, use the env command INFO 320 week 5

  30. Setting environment variables The declare command can set environment variables, or even create new ones Some you can set at the command line, like PS1 PS1=‘/w$’ See the PROMPTING section in the bash manual page for details on prompt characters INFO 320 week 5

  31. Command completion File names, shell variables, and user names can be completed by hitting the tab echo $HOM<tab> cat pre<tab> might yieldcat preface If the command is not unique yet, tab twice to get a list of options echo $H<tab><tab> shows all $H* environment variables INFO 320 week 5

  32. Multiple line commands A command can be stretched across more than one line by ending each preliminary line with a backslash \ grep turtle \ /home/username/folder/file.txt More often used in scripting, this allows messy commands to be more readable Notice that \ and / have wildly different meanings! INFO 320 week 5

  33. Scripting INFO 320 week 5

  34. Bash scripting A series of commands can be put in a text file, and executed as though you typed them in manually – that’s the purpose of a shell script Scripts are used for backups, system monitoring, and many other purposes Here we’re dealing with bash scripts Syntax differs for other shells INFO 320 week 5

  35. File to script? How does a file become a script? We make it executable chmod u+x filename Then to run the script, use the filename like a command filename Or if you’re in a different directory, use the full path name to the script file INFO 320 week 5

  36. File to script? In order for a file to be executed properly as a script, the first line should tell the OS how to execute it #!/bin/bash The #! tells the OS to execute the rest of the line as a command INFO 320 week 5

  37. Comments Oddly enough, by itself a # means the start of a comment, which can appear after commands on a line or from the start of the line ls –l # here’s a same-line comment # this is a separate comment # it’s good to comment what your script does, who wrote it, etc. INFO 320 week 5

  38. Script basics A script can execute normal commands Output from the script can be Create or append files Provide output to the screen using the echo command echo –n keeps text on a single line of output INFO 320 week 5

  39. Bash environment variables There are environment variables which can only be used in the context of a script $0 is the name of the program/script $1 through $9 are the values of arguments passed to the program $# is the number of arguments $$ is the process ID of this process We can also use the other envir var’s $HOME, $USER, etc. INFO 320 week 5

  40. Scripting concepts Scripts can use all the tools we’ve seen Input and/or output redirection Pipes Grep But we can also have conditionals, typically based on the environment variables INFO 320 week 5

  41. if and fi An if statement exists in bash scripting The general syntax is if [ condition1 ] then # commands go here elif [ condition2 ] # more commands else # more commands fi INFO 320 week 5

  42. if and fi The if condition often looks cryptic [ ! -e “$1” ] To see what the options !, –e, -d, etc. mean, look for FUNCTIONS in the bash man page “$1” refers to the environment variable $1 String comparisons are also allowed, e.g. string1 == string2 string1 > string2 Warning! Spaces are critical in the condition! INFO 320 week 5

  43. Other script features We won’t go into them, but other structures can exist in scripts Loops (do, while, until, etc.) Case statements Scripts can be recursive INFO 320 week 5

  44. References CommandlineHowto, https://help.ubuntu.com/community/CommandlineHowto Linux Administration Handbook, by Evi Nemeth et al, Prentice Hall 2007. ISBN 0131480049 Bash man page, http://manpages.ubuntu.com/manpages/jaunty/man1/bash.1.html INFO 320 week 5

More Related