blogger templates blogger widgets
Showing posts with label shell basics. Show all posts
Showing posts with label shell basics. Show all posts
This is part of a list of blog posts.
To browse the contents go to

Shell commandS

Only the few of the most important commands are discussed here.

cd : to change directory.

cd ..
will go one step above.
cd ./temp
is same as cd temp.
cd –
will take you to the previous directory which you used.
cd ~
will take to home directory.
cd /
will take to root directory.


eipe@eipe-system://usr/lib$ cd ~
eipe@eipe-system:~$ cd -
//usr/lib
eipe@eipe-system://usr/lib$


mkdir : to create directory.

mkdir dir1 dir2
will create 2 directories dir1 and dir2 in the current directory.

cp : copy command

cp file1 file2
Copy file1 to file2. If file2 exists, it is overwritten with the contents of file1. If file2 does not exist, it is created. In either case, both files exists.

cp /vol/examples/tutorial/science.txt .
common options are:
-r : to recursively copy directories and their contents. This option (or the -a option) is required when copying directories.
-a : Copy the files and directories and all of their attributes, including ownerships and permissions. Normally, copies take on the default attributes of the user performing the copy.
-i : if file2 exists, the user is prompted before it is overwritten.

cp file1 file2 file3 dir1
Copy file1, file2, file3 into directory dir1. dir1 must already exist.
cp dir1/* dir2
All the files in dir1 are copied into dir2. dir2 must already exist.

mv : move command

mv file1 file2
Move file1 to file2. If file2 exists, it is overwritten with the contents of file1. If file2 does not exist, it
is created. In either case, file1 ceases to exist.

mv file1 file2 dir1
Move file1 and file2 into directory dir1. dir1 must already exist.

rm : to remove files or directories.

rm file1
Delete file1 silently.
rm -i file1
Same as above, except that the user is prompted for confirmation before the deletion is performed.
rm -r file1 dir1
Delete file1 and dir1 and its contents.
rm -f file1
-f option is used to force delete. It means that if the file does exist it will be deleted else if it doesn't no error message is printed simillar to
rm: cannot remove `': No such file or directory
-f option to suppresses these messages. It's useful when u write scripts to delete files silently and automatically at a specific time.

Note:
-v option is used with most of the commands (rm, cp...) it prints a msg about what it had performed.

login
To login as root/super user

sudo su
To login as any other user

login

Shell prompt

This is a cursor that blinks when a terminal window is opened. It shows the position of where the next command/text would be typed. A general format of a shell prompt is username @ machinename : currentworkingdir $ if it's a “#” instead of “$” then that user has superuser privileges.


Command history


As such a terminal does not allows a user to go back and edit the previous command. Instead a user could make use of a feature called “command history” using which he could bring any previous commands to the prompt (using arrow keys) and then edit them as needed. Most Linux distributions remember the last five hundred commands by default.



Virtual terminal


Even if there are no terminals to be seen there are terminals running behind the GUI(Especially if you are using Ubuntu or any GUI enveloped Linux systems). These terminals could be accessed using Ctrl+Alt+F1 through Ctrl+Alt+F6. To switch back to GUI use Ctrl+Alt+F7.



Is shell and terminal the same?


The Wiki defines shell as "A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel." There couldn't be a better definition. All modern operating systems provide a shell so that users could harness the power of the operating system to it's full. Now terminal refers to terminal emulator which is a software that opens a window and lets you interact with the shell. There are a bunch of different terminal emulators you can use. Most Linux distributions supply several, such as: xterm - the standard terminal emulator for the X Window System. konsole - KDE. gnome-terminal - Genome



Customizing the shell prompt

The prompt is defined by an environment variable named PS1 (short for “prompt string one”). We can view the contents of PS1 with the echo command:

[On Ubuntu 10.10]

eipe@eipe-john:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+$debian_chroot)}\u@\h:\w\$


Most of the backslash characters that we see above are escape sequences. They are discussed under the topic escape character sequences.

There are other variables PS2, PS3 and PS4 which are infrequently used by an average user. These are discussed in environment variables tutorial. There is another variable PROMPT_COMMAND. The contents of this variable are executed as a regular shell command just before a shell displays a prompt.

Now, let's modify the PS1 variable to change the appearance of the prompt. But before you do that take a back up of the variable. It is easily done by assigning this to a new variable.

$ ps1original=”$PS1”
$ echo $ps1original


Character color is controlled by sending the terminal an ANSI escape code embedded in the stream of characters to be displayed. These escape code does not get printed on the display, rather it is interpreted by the terminal as an instruction. The \[\033 and \] escape sequences are used to encapsulate non-printing characters.
For colour escape sequences, they should also be followed by a lowercase m. More on sequences in escape sequences tutorial. Common color codes:


Black0;30Dark Gray1;30
Blue0;34Light Blue1;34
Green0;32Light Green1;32
Cyan0;36Light Cyan1;36
Red0;31Light Red1;31
Purple0;35Light Purple1;35
Brown0;33Yellow1;33
Light Gray0;37White1;37


The simplest prompt would be a single character, such as


eipe@eipe-john:~$ PS1=$
$


A more complex styling would be,

eipe@eipe-john:~$ PS1='\[\033[1;36m\][\u:\s \v]\T \w\$:\[\033[0;30m\]'

[eipe:bash 4.1] 10:43:29 ~$:

The reason why \033[0;30m is given in the end is to revert back to another color (in this case black) so that the text which the user types is not in Light Cyan color. \u prints the current user, \s the type of shell and \v it's version. \T prints the current time in 12hr format and \w prints the current working directory. It's also possible to set the text background color using the codes listed below.

0;40mBlack0;44mBlue
0;41mRed0;45mPurple
0;42mGreen0;46mCyan
0;43mBrown0;47mLight Grey

There are lot of other tweaks that one can do using the PS variable. It includes moving the cursor, bold, underscore, blink, etc. To save the value permanently by typing the command,

$export PS1


This will create an entry in .bashrc file. PS1 is the default interaction prompt. Lets see the other prompts that are available.

PS2 - Continuation interactive prompt

A very long unix command can be broken down to multiple line by giving \ at the end of the line. The default interactive prompt for a multi-line command is ">". Let us change this default to display "->" by using PS2 environment variable as shown below.


eipe@eipe-john:~$ echo $PS2
>
eipe@eipe-john:~$ PS2="->"
eipe@eipe-john:~$ echo $PS2
->
eipe@eipe-john:~$ ls \
->-al


PS3 – Prompt used by “select” inside shell scripts

The default is "#?" for select command prompt. Consider a simple shell program
$ vi simpleprog.sh
type in the below program

select i in 1 2 3 exit do
case $i in
1) echo "First";;
2) echo "Second";;
3) echo "Third";;
exit) exit;;
esac done


$ chmod 755 simpleprog.sh
$ ./simpleprog.sh
1) 1
2) 2
3) 3
4) exit
#? 1 First
#? 4

Now to change the default #? prompt. Add the below line to the top of the script.
PS3="Select a num: "


$./simpleprog.sh
1) 1
2) 2
3) 3
4) exit
select a num:4

PS4 – Used by "set -x" to prefix tracing output

The PS4 variable defines the prompt that gets displayed, when you execute a shell script in debug mode as shown below. Type in the below shell script into a file simpleprog.sh

set -x echo "the number of png files in home" ls /home/eipe/Documents/* | grep "png" | wc -l


eipe@eipe-john:~$ ./simpleprog.sh
++ echo 'the number of png files in home' the number of png files in home
++ wc -l
++ grep png
++ ls 4
eipe@eipe-john:~$

To change the default to something else use the below line above the sh file.
PS4="$LINENO@ "

$LINENO is a variable that stores the current line number.

eipe@eipe-john:~$ ./simpleprog.sh
11@ echo 'the number of png files in home' the number of png files in home
11@ wc -l
11@ ls
11@ grep png 4

PROMPT_COMMAND

The contents of this variable are executed as a regular Bash command just before Bash displays a prompt.

eipe@eipe-john:~$ PROMPT_COMMAND="date"
Sat Jan 29 10:23:33 IST 2011
eipe@eipe-john:~S
Sat Jan 29 10:23:33 IST 2011
eipe@eipe-john:~S

Advanced prompt editing

You can use the output of regular Linux commands directly in the prompt as well. Obviously, we don't want to insert a lot of material, or it will create a large prompt.Linux comes with a lot of small utility programs like date, grep, or wc that allow you to manipulate data.
If you find yourself trying to create complex combinations of these programs within a prompt, it may be easier to make an alias, function, or shell script of your own, and call it from the prompt. We will see this in separate tutorials

Shell - The Basics

Shell
It is a program that takes keyboard inputs and passes it to the operating system for processing.This program works in close interaction with the operating system (kernal, to be specific) and it is this functionality that brought this tiny program to the limelight.

Almost all Linux distributions supply a shell program from the GNU Project called bash. The name “bash” is an acronym for “Bourne Again SHell”, a reference to the fact that bash is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.

Types of Shells

Shell TypesDescription
sh (Bourne)The original shell from early versions of UNIX
csh, tcsh zshThe C shell originally created by Bill Joy, and its derivatives – Tenex C shell and Z shell.
ksh, pdkshThe Korn shell and its public domain cousin. Written by David Korn, this is the default shell on many commercial UNIX versions.
bashThe Linux shell from the GNU project. bash or Bourne Again SHell, has the advantage that the source code is freely available. bash has many similarities to the Korn shell.

Terminal

To interact with shell we need a terminal emulator when using GUI desktops like KDE or GNOME.
For KDE we have konsole. For GNOME we have gnome-terminal.

Commands we use inside a terminal could be any of these:
  • An executable program. I.e, files in /usr/bin. These files are compiled binaries of programs written in C, C++, or any other language.
  • A command built into the shell itself. bash supports a number of commands internally called shell builtins. Eg: cd
  • A shell function. These are shell scripts incorporated into the environment.
  • An user created command also called as alias. These are commands defined by the user but actually built from other commands.


Difference between a terminal, shell, tty and a console?
Actually there is already a very good explanation posted in one of my favorite sites. Unix.StackExchage