The shell is perhaps the most important program on the Unix system, from the end-user's standpoint.
The shell is your interface with the Unix system, the middleman between you and the kernel.
CONCEPT: The shell is a type of program called an interpreter. An interpreter operates in a simple loop: It accepts a command, interprets the command, executes the command, and then waits for another command. The shell displays a "prompt," to notify you that it is ready to accept your
command.
The shell is a program that the Unix kernel runs for you. A program is referred to as a process while the kernel is running it. The kernel can run the same shell program (or any other program) simultaneously for many users on a Unix system, and each running copy of the program is a separate process.
The basic form of a Unix command is: commandname [-options] [arguments]
ls -l /tmp
-l = Long list of options
ls -lR /lib/l*
-R causes ls to operate recursively, moving down directory trees
ls -m /etc/i*g
-m causes output to be streamed into a single line.
-l Shows you huge amounts of information (permissions, owners, size, and when last modified.)
-r Reverses the order of how the files are displayed.
-t Shows you the files in modification time.
Special Characters in Unix: `,~,!,$,%,^, & *, |, \,/, {,},[,],",',;
your file names cannot start with any of the spl characters above.
Getting Help in Unix:
man
whatis
File Permissions:
ls -l /etc/passwd
-rw-r--r-- 1 root sys 41002 Apr 17 12:05 /etc/passwd
first - (is a - if its a normal file, d, if its a directory and s if its a special file ex: device file)
next 3: rw- : permissons of the owner
next 3: r-- : Permissions of the group
next 3: r-- : permissions of others
To set file permissions, you may use to the "rwx" notation to specify the type of permissions, and the
"ugo" notation to specify those the permissions apply to.
To define the kind of change you want to make to the permissions, use the plus sign (+) to add a permission, the minus sign (-) to remove a permission, and the equal sign (=) to set a permission directly.
chmod g=rw- ~/.shrc (here you specify / set all permissions)
to change the file permissions on the file .shrc, in your home directory. Specifically, you are
specifying group read access and write access, with no execute access.
u : owner
g : group
o : others
a : all
chmod a-x socktest.pl (revoke execute permissions for all on a file)
$ ls -l socktest.pl
-rw-r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl
chmod 755
You might have encountered things like chmod 755 somefile and of course you will be wondering what this is. The thing is, that you can change the entire permission pattern of a file in one go using one number like the one in this example. Every mode has a corresponding code number, and as we shall see there is a very simple way to figure out what number corresponds to any mode.
Triplet for u: rwx => 4 + 2 + 1 = 7
Triplet for g: r-x => 4 + 0 + 1 = 5
Tripler for o: r-x => 4 + 0 + 1 = 5
Which makes : 755
So, 755 is a terse way to say 'I don't mind if other people read or run this file, but only I should be able to modify it' and 777 means 'everyone has full access to this file'
pwd : Print Working Directory
file is a standard Unix program for determining the type of data contained in a computer file.
The Unix file command allows you to determine whether an unknown file is in text format, suitable for direct viewing
file /bin/sh
The cat command
The cat command concatenates files and sends them to the screen. You can specify one or more files as arguments. Cat makes no attempt to format the text in any way, and long output may scroll off the screen before you can read it.
Output scrolls off of the screen
The tilde character (~) is Unix shorthand for your home directory
The more command
The more command displays a text file, one screenful at a time. You can scroll forward a line at a time by pressing the return key, or a screenful at a time by pressing the spacebar. You can quit at any time by pressing the q key.
head -15 /etc/rc
to see the first fifteen lines of the /etc/rc file.
tail /etc/rc
to see the last ten lines of the file /etc/rc. Because we did not specify the number of lines as an
option, the tail command defaulted to ten lines.
cp ~/.profile ~/pcopy
makes a copy of your .profile file, and stores it in a file called "pcopy" in your home directory.
mv ~/pcopy ~/qcopy
takes the pcopy file you created in the cp exercise, and renames it "qcopy".
The rm command is used for removing files and directories. The syntax of the rm command is rm
filename. You may include many filenames on the command line.
rm ~/.shrccopy
The Unix mkdir command is used to make directories. The basic syntax is mkdir directory-name.
If you do not specify the place where you want the directory created (by giving a path as part of the
directory name), the shell assumes that you want the new directory placed within the current working
directory.
mkdir ~/foo
The Unix rmdir command removes a directory from the filesystem tree. The rmdir command does not work unless the directory to be removed is completely empty.
The rm command, used with the -r option can also be used to remove directories. The rm -r command will first remove the contents of the directory, and then remove the directory itself.
Redirecting Input and Output
< = Input redirection
> Output redirection to file
>> output redirection to file (append)
2> error redirection
ex: more < /etc/passwd
Use standard input redirection to send the contents of the file /etc/passwd to the morecommand
Using the "less-than" sign with a file name like this:
< file1 in a shell command instructs the shell to read input from a file called "file1" instead of from the keyboard.
To see the first ten lines of the /etc/passwd file, the command:
head /etc/passwd
will work just the same as the command:
head < /etc/passwd
ls /tmp > ~/ls.out
ls /etc >> myls
sort < /etc/passwd > foo 2> err
Pipe:
CONCEPT: Unix allows you to connect processes, by letting the standard output of one process feed into the standard input of another process. That mechanism is called a pipe. Connecting simple processes in a pipeline allows you to perform complex tasks without writing complex programs.
ls -l /etc | more
How could you use head and tail in a pipeline to display lines 25 through 75 of a file?
ANSWER: The command
cat file | head -75 | tail -50
The grep utility recognizes a variety of patterns, and the pattern specification syntax was taken from
the vi editor. Here are some of the characters you can use to build grep expressions:
> The caret (^) matches the beginning of a line.
> The dollar sign ($) matches the end of a line.
> The period (.) matches any single character.
> The asterisk (*) matches zero or more occurrences of the previous character.
> The expression [a-b] matches any characters that are lexically between a and b.
grep 'jon' /etc/passwd
grep '^jon' /etc/passwd
ls -l /tmp | grep 'root'
PS:
Unix provides a utility called ps (process status) for viewing the status of all the unfinished jobs that
have been submitted to the kernel. The ps command has a number of options to control which
processes are displayed, and how the output is formatted.
ps -ef
to see a complete listing of all the processes currently scheduled. The -e option causes ps to include
all processes (including ones that do not belong to you), and the -f option causes ps to give a long
listing. The long listing includes the process owner, the process ID, the ID of the parent process,
processor utilization, the time of submission, the process's terminal, the total time for the process,
and the command that started the process.
To kill a process, you must first find its process ID number using the ps command. Some processes
refuse to die easily, and you can use the "-9" option to force termination of the job.
EXAMPLE: To force termination of a job whose process ID is 111, enter the command
kill -9 111
Executing a job in background
sort < foo > bar &
The commands related to job control will apply to
the current job, unless directed to do otherwise. You may refer to jobs by job ID by using the percent
sign. Thus, job 1 is referred to as %1, job 2 is %2, and so forth.
To place a foreground job to background, use bg command
No comments:
Post a Comment