Presentation is loading. Please wait.

Presentation is loading. Please wait.

System Programming and administration CS 308

Similar presentations


Presentation on theme: "System Programming and administration CS 308"— Presentation transcript:

1 System Programming and administration CS 308
LINUX COMMANDS Lecture 8

2 UNIX Commands Linux Commands
A command is a program which interacts with the kernel to provide the environment and perform the functions called for by the user. A command can be: a built-in shell command; an executable shell file, known as a shell script; or a source compiled, object code file. The shell is a command line interpreter. The user interacts with the kernel through the shell. You can write ASCII (text) scripts to be acted upon by a shell.

3 UNIX Shell Linux Commands
The shell sits between you and the operating system, acting as a command interpreter. It reads your terminal input and translates the commands into actions taken by the system. The shell is analogous to command.com in DOS. When you log into the system you are given a default shell. When the shell starts up it reads its startup files and may set environment variables, command search paths, and command aliases, and executes any commands specified in these files.

4 UNIX Shell Linux Commands The original shell was the Bourne shell, sh.
Every Unix platform will either have the Bourne shell, or a Bourne compatible shell available. The default prompt for the Bourne shell is $ (or #, for the root user). Another popular shell is C Shell. The default prompt for the C shell is %.

5 UNIX Shell Linux Commands
Numerous other shells are available most of them are based on either sh or csh with extensions to provide job control to sh. Some of the more well known of these are: the Korn shell, ksh, by David Korn and the Bourne Again SHell, bash, from the Free Software Foundations GNU project, both based on sh, the T-C shell, tcsh, and the extended C shell, cshe, both based on csh.

6 Shell Programming Linux Commands
You can write shell programs by creating scripts containing a series of shell commands. The first line of the script should start with #! which indicates to the kernel that the script is directly executable. You immediately follow this with the name of the shell, or program (spaces are allowed), to execute, using the full path name. So to set up a Bourne shell script the first line would be: #! /bin/sh

7 Shell Programming Linux Commands
The first line is followed by commands Within the scripts # indicates a comment from that point until the end of the line, with #! being a special case if found as the first characters of the file. #!/bin/bash cd /tmp mkdir t You also need to specify that the script is executable by setting the proper bits on the file with chmod, e.g.: $ chmod +x shell_script

8 LINUX COMMANDS LINUX Linux Commands File Management and Viewing
Filesystem Mangement Help, Job and Process Management Network Management System Management User Management Printing and Programming Document Preparation Miscellaneous

9 Command Structure Linux Commands
Command <Options> <Arguments> Multiple commands separated by ; can be executed one after the other $ date; pwd; ls > execute three commands in sequence. Mon Feb :11:10 CST 1998 /home/glass/wild a.c b.c cc.c dir dir2 $ _

10 Help Facilities for Commands
Linux Commands Help Facilities for Commands To understand the working of the command and possible options use (man command) Using the GNU Info System (info, info command) Listing a Description of a Program (whatis command) Many tools have a long−style option, `−−help', that outputs usage information about the tool, including the options and arguments the tool takes. Ex: whoami --help

11 Linux Commands Pipes An important early development in Unix was the invention of "pipes," a way to pass the output of one tool to the input of another. Shells allow you to use the standard output of one process as the standard input of another process by connecting the processes together using the pipe(|) metacharacter. eg. $ who | wc −l By combining these two tools, giving the wc command the output of who, you can build a new command to list the number of users currently on the system

12 Pipes Linux Commands $ cat /etc/passwd | awk -F: ‘{ print $1 }’ | sort
Here’s an illustration of the pipeline that we just used: ls Pipe awk Pipe sort Terminal In the example, we pipe the contents of the “/etc/passwd” file into the awk utility to extract the first field of each line. The output of awk is then piped to the sort utility, which sorts the lines alphabetically. The result is a sorted list of every user on the system.

13 SELECTING A SHELL Linux Commands
When you are provided with a UNIX account, the system administrator chooses a shell for you. To find out which shell was chosen for you, look at your prompt. If you have a $ prompt, you’re probably in a Bash, Bourne or a Korn shell. If you have a % prompt, you’re probably in a C shell.

14 SELECTING A SHELL Linux Commands  Utility : chsh
- chsh allows you to change your default login shell. It prompts you for the full pathname of the new shell, which is then used as your shell for subsequent logins.(Q) - In order to use chsh, you must know the full pathnames of the three shells. Here they are: Shell Full pathname Bourne /bin/sh Bash /bin/bash Korn /bin/ksh C /bin/csh

15 SELECTING A SHELL Linux Commands
In the following example, we change the default login shell from a Bourne shell to a Bash shell: $ chsh > change the login shell from sh to bash. Changing login shell for glass Old shell : /bin/sh > pathname of old shell is displayed. New shell: /bin/bash ---> enter full pathname of new shell. $ ^D > terminate login shell. login : glass > log back in again. password : > secret. $ _ > this time we are in a bash shell. Another way to find out the full pathname of your login shell is to type the following: $ echo $SHELL ---> display the name of current login shell. /bin/bash > full pathname of the Korn shell. $ - This example illustrated the echo shell command and a shell variable called SHELL.

16 SHELL OPERATIONS Linux Commands
When a shell is invoked, either automatically during a login or manually from a keyboard or script, it follows a preset sequence: 1. It reads a special startup file, typically located in the user’s home directory, that contains some initialization information. Each shell’s startup sequence is different, so we’ll leave the specific details for later. 2. It displays a prompt and waits for a user command. 3. If the user enters a Control-D character on a line of its own, this command is interpreted by the shell as meaning “end of input”, and it causes the shell to terminate; otherwise, the shell executes the user’s command and returns to step 2.

17 SHELL OPERATIONS Linux Commands
Commands range from simple utility invocations like: $ ls to complex-looking pipeline sequences like: $ ps -ef | sort | ul -tdumb | lp - a command with a backslash(\) character, and the shell will allow you to continue the command on the next line: $ echo this is a very long shell command and needs to \ be extended with the line-continuation character. Note \ that a single command may be extended for several lines. $ _

18 EXECUTABLE FILES VERSUS BUILT-IN COMMANDS
Linux Commands EXECUTABLE FILES VERSUS BUILT-IN COMMANDS Most UNIX commands invoke utility programs that are stored in the directory hierarchy. Utilities are stored in files that have execute permission. For example, when you type $ ls the shell locates the executable program called “ls”, which is typically found in the “/bin” directory, and executes it.

19 EXECUTABLE FILES VERSUS BUILT-IN COMMANDS
Linux Commands EXECUTABLE FILES VERSUS BUILT-IN COMMANDS Displaying Information : echo The built-in echo command displays its arguments to standard output and works like this: Shell Command: echo {arg}* echo is a built-in shell command that displays all of its arguments to standard output. By default, it appends a new line to the output. Changing Directories : cd The built-in cd command changes the current working directory of the shell to a new location. METACHARACTERS Some characters are processed specially by a shell and are known as metacharacters. All four shells share a core set of common metacharacters, whose meanings are as follow:

20 EXECUTABLE FILES VERSUS BUILT-IN COMMANDS
Linux Commands EXECUTABLE FILES VERSUS BUILT-IN COMMANDS Symbol Meaning `command` Command substitution; replaced by the output from command. | Pipe symbol; sends the output of one process to the input of another. ; Used to sequence commands. || Conditional execution; executes a command if the previous one fails. && Conditional execution; executes a command if the previous one succeeds. (…) Groups commands. & Runs a command in the background. # All characters that follow up to a new line are ignored by the shell and program(i.e., used for a comment) $ Expands the value of a variable. \ Prevents special interpretation of the next character. <<tok Input redirection; reads standard input from script up to tok.

21 EXECUTABLE FILES VERSUS BUILT-IN COMMANDS
Linux Commands EXECUTABLE FILES VERSUS BUILT-IN COMMANDS - When you enter a command, the shell scans it for metacharacters and processes them specially. When all metacharacters have been processed, the command is finally executed. To turn off the special meaning of a metacharacter, precede it by a backslash(\) character. Here’s an example: $ echo hi > file ---> store output of echo in “file”. $ cat file > look at the contents of “file”. hi $ echo hi \> file ---> inhibit > metacharacter. $ cat file > look at the file again. hi > file > > is treated like other characters. $ _

22 Linux Commands Shell Redirection • Redirection
The shell redirection facility allows you to: 1) store the output of a process to a file ( output redirection ) 2) use the contents of a file as input to a process ( input redirection ) Output redirection To redirect output, use either the “>” or “>>” metacharacters. The sequence $ command > fileName sends the standard output of command to the file with name fileName. The shell creates the file with name fileName if it doesn’t already exist or overwrites its previous contents if it does already exist.

23 Linux File Management and Viewing
Linux Commands Linux File Management and Viewing There are three such special permissions within Linux. They are: setuid — used only for applications, this permission indicates that the application is to run as the owner of the file and not as the user executing the application. It is indicated by the character s in place of the x in the owner category. If the owner of the file does not have execute permissions, the S is capitalized to reflect this fact. setgid — used primarily for applications, this permission indicates that the application is to run as the group owning the file and not as the group of the user executing the application. The setgid permission is indicated by the character s in place of the x in the group category. If the group owner of the file or directory does not have execute permissions, the S is capitalized to reflect this fact.

24 Linux File Management and Viewing
Linux Commands Linux File Management and Viewing There are three such special permissions within Linux. They are: sticky bit — used primarily on directories, this bit dictates that a file created in the directory can be removed only by the user that created the file. It is indicated by the character t in place of the x in the everyone category. If the everyone category does not have execute permissions, the T is capitalized to reflect this fact.

25 Linux File Management and Viewing
Linux Commands Linux File Management and Viewing chown Change owner. Ex: chown <owner1> <filename> : Change ownership of a file to owner1. chgrp Change group. Ex: chgrp <group1> <filename> : Change group of a file to group1. chmod Change the file permissions. Ex: chmod 751 myfile : change the file permissions to rwx for owner, rx for group and x for others Ex: chmod go=+r myfile : Add read permission for the group and others (character meanings u-user, g-group, o-other, + add permission, -remove, r-read, w-write, x-exe) Ex: chmod +s myfile - Setuid bit on the file which allows the program to run with user or group privileges of the file.

26 VARIABLES Linux Commands
A shell supports two kinds of variables: local and environment variables. Both kinds of variables hold data in a string format. Environment variables are therefore used for transmitting useful information between parent shells and their children. Every shell has a set of predefined environment variables that are usually initialized by the startup files.

27 VARIABLES Linux Commands
Here is a list of the predefined environment variables that are common to all shells: Name Meaning $HOME the full pathname of your home directory $PATH a list of directories to search for commands $MAIL the full pathname of your mailbox $USER your username $SHELL the full pathname of your login shell $TERM the type of your terminal

28 VARIABLES Linux Commands
The syntax for assigning variables differs between shells, but the way that you access the variables is the same: If you precede the name of a variable with a $, this token sequence is replaced by the shell with the value of the named variable. To create a variable, simply assign it a value; variable does not have to be declared. the syntax for assigning a variable in the Bourne, Bash and Korn shells is as follows: variableName=value ---> place no spaces around the value or variableName=“ value ” ---> here, spacing doesn’t matter

29 VARIABLES Linux Commands
In the following example, we display the values of some common shell environment variables: $ echo HOME = $HOME, PATH=$PATH > list two variables. HOME =/home/glass, PATH=/bin:/usr/bin:/usr/sbin $ echo MAIL = $MAIL MAIL=/var/mail/glass $ echo USER = $USER, SHELL = $SHELL, TERM=$TERM USER = glass, SHELL = /bin/sh, TERM=vt100 $ _

30 Questions??????


Download ppt "System Programming and administration CS 308"

Similar presentations


Ads by Google