Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.ischool.drexel.edu INFO 320 Server Technology I Week 5 Shell environments and scripting 1INFO 320 week 5.

Similar presentations


Presentation on theme: "Www.ischool.drexel.edu INFO 320 Server Technology I Week 5 Shell environments and scripting 1INFO 320 week 5."— Presentation transcript:

1 www.ischool.drexel.edu INFO 320 Server Technology I Week 5 Shell environments and scripting 1INFO 320 week 5

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

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

4 www.ischool.drexel.edu 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 % 4INFO 320 week 5

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

6 www.ischool.drexel.edu 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 6INFO 320 week 5

7 www.ischool.drexel.edu 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 7INFO 320 week 5

8 www.ischool.drexel.edu Aliases and links 8INFO 320 week 5

9 www.ischool.drexel.edu Aliases We can define a shortcut for a command in the form of an aliasalias –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 9INFO 320 week 5

10 www.ischool.drexel.edu 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 10INFO 320 week 5

11 www.ischool.drexel.edu 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 11INFO 320 week 5

12 www.ischool.drexel.edu 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 12INFO 320 week 5

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

14 www.ischool.drexel.edu 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 14INFO 320 week 5

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

16 www.ischool.drexel.edu 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 16INFO 320 week 5

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

18 www.ischool.drexel.edu 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 more command forces display of one page of text at a time more –So ls –al | more does what? 18INFO 320 week 5

19 www.ischool.drexel.edu Pipes and grep Pipes are great for use with grep grep By itself, grep string file searches 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 19INFO 320 week 5

20 www.ischool.drexel.edu Pipes into sorting, head or tail Pipes can feed sort, head, or tail –sort does what it sounds likesort –head gives the first ten lines of the filehead –tail gives the last ten lines of the filetail Great for looking at the end of a log file, for example 20INFO 320 week 5

21 www.ischool.drexel.edu 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 21INFO 320 week 5

22 www.ischool.drexel.edu 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 22INFO 320 week 5

23 www.ischool.drexel.edu 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 23INFO 320 week 5

24 www.ischool.drexel.edu 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 24INFO 320 week 5

25 www.ischool.drexel.edu touch The touch command is an oddity touch –“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 25INFO 320 week 5

26 www.ischool.drexel.edu Environment variables 26INFO 320 week 5

27 www.ischool.drexel.edu 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 27INFO 320 week 5

28 www.ischool.drexel.edu 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 28INFO 320 week 5

29 www.ischool.drexel.edu 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 29INFO 320 week 5

30 www.ischool.drexel.edu 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 charactersbash manual page 30INFO 320 week 5

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

32 www.ischool.drexel.edu 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! 32INFO 320 week 5

33 www.ischool.drexel.edu Scripting 33INFO 320 week 5

34 www.ischool.drexel.edu 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 purposesmany other purposes Here we’re dealing with bash scripts –Syntax differs for other shells 34INFO 320 week 5

35 www.ischool.drexel.edu 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 35INFO 320 week 5

36 www.ischool.drexel.edu 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 36INFO 320 week 5

37 www.ischool.drexel.edu 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. 37INFO 320 week 5

38 www.ischool.drexel.edu 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 38INFO 320 week 5

39 www.ischool.drexel.edu 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. 39INFO 320 week 5

40 www.ischool.drexel.edu 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 40INFO 320 week 5

41 www.ischool.drexel.edu 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 41INFO 320 week 5

42 www.ischool.drexel.edu 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 pagebash 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! 42INFO 320 week 5

43 www.ischool.drexel.edu 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 43INFO 320 week 5

44 www.ischool.drexel.edu References CommandlineHowto, https://help.ubuntu.com/community/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 http://manpages.ubuntu.com/manpages/jaunty/man1/bash.1.html 44INFO 320 week 5


Download ppt "Www.ischool.drexel.edu INFO 320 Server Technology I Week 5 Shell environments and scripting 1INFO 320 week 5."

Similar presentations


Ads by Google