UNIX-2

SCP

SCP stands for secure copy is used to copy data (files or directories) from one unix or linux system to another unix or linux server. SCP uses secured shell (ssh) to transfer the data between the remote hosts. The features of SCP are:

  • Copies files within in the same machine
  • Copies files from local machine to remote machine.
  • Copies files from remote machine to local machine.
  • Copies files between two different remote servers.

SCP Command Syntax:

The syntax of SCP command is

scp [Options] [[User@]From_Host:]Source_File [[User@]To_Host:][Destination_File]

Each element of the scp command is explained in detail below:

  • User is the one who have the permissions to access the files and directories. User should have read permissions if it is a source and write permissions if it is the destination.
  • From_Host: hostname or Ip address where the source file or directory resides. This is optional if the from host is the host where you are running the scp command.
  • Source_File: Files or directories to be copied to the destination.
  • To_Host: Destination host where you want to copy the files. You can omit this when you want to copy the files to the host where you are issuing the scp command.
  • Destination_File: Name of the file or directory in the target host.

SCP Command Options:

The important SCP command options are listed below:

  • -r : Recursively copies the contents of source files or directories.
  • -p : Preserves the access time, modification time, permissions of the source files in the destination.
  • -q : Progress bar in not displayed
  • -v : verbose mode. Displays debugging messages.
  • -P : copy files using the specified port number.

SCP Command Examples:

Let see the examples of scp command in unix or linux system.

. Copying with in the same system

You can use the scp command just like the cp command to copy files from one directory to another directory.

scp Unix-storage.dat /var/tmp/

This command copies the file unix-storage.dat from current directory to the /var/tmp directory.

. Copy file from local host to remote server

This is most frequently used operation to transfer files in unix system.

scp filename user@remotehost:/remote/directory/

This command connects to the remote host and copies the specified file to the /remote/directory/.

. Copy files from remote host to local server.

This operation is used when taking backup of the files in remote server.

scp user@remotehost:/usr/backup/oracle_backup.dat .

This command copies the oracle backup file in the remote host to the current directory.

. Copying files between two remote servers

The scp command can also be used to copy files between two remote hosts.

scp source_user@source_remote_host:/usr/bin/mysql_backup.sh 
target_user@target_remote_host:/var/tmp/

The above command copies the mysql bakup shell script from the source remote host the /var/tmp directory of target remote host.

. Copying a directory.

To copy all the files in a directory, use the -r option with the scp command. This makes the scp command to copy the directory recursively.

scp -r directory user@remotehost:/var/tmp/

The above command copies the directory from local server to the remote host.

. Improving performance of scp command

By default the scp command uses the Triple-DES cipher/AES-128 to encrypt the data. Using the blowfish or arcfour encryption will improve the performance of the scp command.

scp -c blowfish filename  user@remoteserver:/var/
scp -c arcfour  localfile user@remoteserver:/var/

. Limit bandwidth

You can limit the bandwidth used by the scp command using the -l option.

scp -l bandwidth_limit filename user@hostname:/usr/backup/
Here bandwidth_limit is numeric to be specified in kilobits per second.

. Specifying the port number

We can make the scp command to copy the files over a specified port number using the -P option.

SSH

SSH client utility in unix or linux server is used to logging into a remote host and execute commands on the remote machine. The rlogin and rsh commands can also be used to login into the remote machine. However these are not secure. The ssh command provides a secure connection between two hosts over a insecure network.

 

ssh [-l username] hostname | user@remote-hostname [command]

Let see the examples of ssh command.

SSH Command Examples:

. Logging to a remote server

You can login to a remote server from the local host as shown below:

localhost:[~]> ssh -l username remote-server
username@remote-server password:
remote-server:[~]>

Alternatively you can use the below ssh command for connecting to remote host:

localhost:[~]> ssh username@remote-server
username@remote-server password:
remote-server:[~]>

Note: If you are logging for the first time, then it will prints a message that host key not found and you can give yes to continue. The host key of the remote server will be cached and added to the .ssh2/hostkeys directory in your home directory. From second time onwards you just need to enter the password.

. Logging out from remote server

Simply enter the exit command on the terminal to close the connection. This is shown below:

remote-server:[~]>exit
logout
Connection to remote-server closed.
localhost:[~]>

. Running remote commands from local host

Sometimes it is necessary to run the unix commands on the remote server from the local host. An example is shown below:

localhost:[~]> ssh user@remote-host "ls test"
online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt

The ssh command connects to the remote host, runs the ls command, prints the output on the local host terminal and exits the connection from remote host.

Let see whether the ls command actually displayed the correct result or not by connecting to the remote host.

localhost:[~]> ssh user@remote-host
user@remotehost password:
remotehost:[~]> cd test
remotehost:[~/test]> ls
online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt

. Version of the SSH command

We can find the version of SSH installed on the unix system using the -V option to the ssh. This is shown below:

> ssh -V
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008

. Debugging the SSH Client

When we are not able to connect to the remote host, it is good to debug and find the exact error messages that causing the issue. Use the -v option for debugging the ssh client.

ssh -v user@remote-host
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to remote-host [172.22.200.140] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/identity type -1
debug1: identity file /home/user/.ssh/id_rsa type -1
debug1: identity file /home/user/.ssh/id_dsa type 2
debug1: loaded 3 keys
..........
..........

. Copying files between remote host and local host.

We can use the scp command to copy the files securely between the local host and remote host using the ssh authentication.

To copy the file from local host to remote hosts /var/tmp/ directory, run the below scp command.

scp filename user@remote-host:/var/tmp/

To copy the file from remote hosts /usr/local/bin/ directory to local hosts current directory, run the below scp command.

scp user@remote-host:/usr/local/bin/add.sh .

CHMOD

Chmod (change mode) is one of the most frequently used commands in unix or linux operating system. The chmod command is used to change the file or directory access permissions. To know about the access permissions of a file or directory, use the ls -l command as shown below:

$ ls -l sample.sh
-rwx-rw-r-- 1 matt deploy 94 Oct  4 03:12 sample.sh

Here in the above example: Use matt has the read, write and execute permissions on the file. Group deploy has read and write permissions. Others have only the read permission.
File and Directory permissions:

There are three different permissions. They are:

  • Read (4): Permitted to read the contents of the file. In case of directory, you can view all the files and sub-directories in that directory.
  • Write (2): Permitted to write to the file. In case of directory, you can create files and sub-directories.
  • Execute (1): Execute the file as a program/shell script. In case of directory, You can enter into that directory.

Here in the above, the numbers in the brackets represents the numeric values for the corresponding permissions. If you want to have a combination of permissions add the required numbers. For example, for read and execute, it is 4+1=5.

The syntax of chmod command is

chmod [options] mode filename

THe important options are:

-R : recursively change the permissions of a directory.
-v : Verbose

Chmod Examples in Linux / Unix:

. Give read, write and execute permissions to everyone.

Read, write and execute: 4+2+1=7

$ chmod 777 sample.sh

In the above example, you can see that the permissions are specified with a three digit number. The first digit is for user permissions, second is for group and third is for others permission. This type of representation is called octal representation. Alternatively, you can use the symbolic representation to give the permissions.

chmod ugo+rwx sample.sh

We will see more details about the symbolic representation later.

. Give read permission to user, write permission to group and execute permission to others.

$ chmod 421 sample.sh

. Recursive permissions to directory

To give read and write permissions to all the users to a directory (including files and subdirectories) use the recursive option -R.

chmod -R 666 /dir

Symbolic Representation of Permissions:

The following symbols are used to represent the users, groups and others:

  • u : User
  • g : Group
  • o : Others a : All (user, group and others)

The following symbols represent the permissions:

  • r : read
  • w : write
  • x : execute

The following symbols represent the permissions grant or revoke:

  • + : Additional permissions. Selected permissions are added.
  • – : Revoke the permissions. Selected permissions are revoked.
  • = : Specific permissions. Only selected permissions are assigned.

Examples:

. Remove write permission from group

$ chmod g-w sample.sh

This will only removes the write permission for the group.

. Add new permission execute to others

$ chmod o+x sample.sh

In addition to the existing permissions, this will add execute permission to others.

. Give only read permissions to the user

$ chmod u=w sample.sh

This will remove the existing permissions to the user and gives only write permission to the user

Use ! For executing last command

This has saved my 30% time on average. It always happens that you fire same UNIX command multiple times within a fraction of seconds, before knowing this trick I used to use up and down arrow for finding my command and then executing them which takes some of my time but after

Knowing this trick I just have to remember command name e.g. !ls will execute your last “ls -lrt” , !vim will open your last file without

Typing full commands. Use it and experience it , It definitely save loads of time and its also useful on shell other than bash where up and down arrow generally doesn’t give you previous commands.

For example After doing ls –l stocks.txt if you want to open stocks.txt you can use vi !$ (last argument)

Use !! for executing last command

This is an extension of previous tip which is used to execute the very last command you have executed. Since it just involves two keystrokes and that too for same key it’s amazingly fast. This will also works on the shells in which up and down arrow don’t work.This is extremely useful if you are stopping or starting your trading application for debug purpose frequently

Use “CRTL+R” for repeating the last matching command

if you remember your last command executed sometime back and just want to find that command with same argument and execute. This is the tip you need to remember. just press “CRTL+R” and type words that you had in your last command and UNIX will find that command for you then just press enter.

 

 Using history command for getting some of the most frequently used UNIX command

This is the first thing I learn when I started working on UNIX 🙂 This is your most helpful command in UNIX or shell. In most of the Cases there are certain command like starting, stopping, checking log files, making build or doing release. Which we need to execute very frequently and if you don’t remember exact command no need to worry, just do history | grep “keyword” and you will get that command from

UNIX history. There are certain environment variable e.g. HISTSIZE which defines how many command UNIX history can store, so have it big

Enough to save your time and avoid referencing your command booklet now and then.

 Using regular expression in grep and find.

grep and find is two best tools UNIX provide to us. almost everybody needs to search something in UNIX e.g. a file , a directory , certain words in file e.g. ERROR or Exception and if you know how to use grep and find with regular expression you will save lot of your time by typing less commands.

For example by knowing about egrep you can fire egrep “ERROR|Exception” *.xml instead of firing two grep command for finding ERROR and Exception individually.

 Using pipe instead of firing two commands

Just shown above this nice and little tip I guess everybody knows 🙂

 Using aliases and defining them in bash profile or bashrc file.

Have you seen some strange commands working in someone’s machine and not yours, which might be aliases he would have setup in either his .bashrc or .profile file. Always do such kind of setup for commonly used command. There are lots of usage of .bashrc and .profile file but

One of the most important one is setting up aliases e.g. “l.” which finds all hidden files. “ls” which includes all useful option e.g. -lrtH to show all relevant information.

Using pushd, popd , cd – , ~ for moving across directory.

Based on my experience navigation in UNIX shell takes almost 50% times of people and if you are going to write directory path every now and then just forget about working fast. so instead of typing full name use all above tips and make best use of pushd, popd, cd – and cd ~ command. cd – is best if your switching between two directory location in UNIX.

Minimize the key strokes or increase the speed of typing.

That I guess you know isn’t it the less you type the more fast you work so make use of your last typed command, make use of tab in bash so that let the UNIX bash shell complete your command, use Ctrl+R if the last command you have typed is very long and you want to change just few lines.other option to get old commands

10) Try to learn more commands and their options and usage this will reduce thinking time for a particular task and use ctrl+z and fg and bg to suspend a process. it saves almost 10% time if you are viewing multiple files or log files so instead of every now and then executing vim commands just do Ctrl+Z to suspend it and fg 1 or fg 2 to bring it on foreground.

Write command to list all the links from a directory?

ls -lrt | grep “^l”

Create a read-only file in your home directory?

$ touch file $ chmod 400 file
How will you find which operating system your system is running on in UNIX?

By using command “uname -a” in UNIX

How will you run a process in background? How will you bring that into foreground and how will you kill that process?

For running a process in background use “&” in command line. For bringing it back in foreground use command “fg jobid” and for getting job id you use command jobs, for killing that process find PID and use kill -9 PID command.

How do you know if a remote host is alive or not?

You can check these by using either ping or telnet command in UNIX.

How do you see command line history in UNIX?

Very useful indeed, use history command along with grep command in UNIX to find any relevant command you have already executed.

How do you copy file from one host to other?

Many options but you can say by using “scp” command. You can also use rsync command  sftp would be ok.

How do you find which process is taking how much CPU?

By using “top” command in UNIX,

How do you check how much space left in current drive ?

By using “df” command in UNIX. For example “df -h .” will list how full your current drive is.

What is the difference between Swapping and Paging?

Swapping:

Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.

Paging:

Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory. It provides greater flexibility in mapping the virtual address space into the physical memory of the machine. Allows more number of processes to fit in the main memory simultaneously. Allows the greater process size than the available physical memory. Demand paging systems handle the memory more flexibly.

What is difference between ps -ef and ps -auxwww?

issue while ago where one culprit process was not visible by execute ps –ef command and we are wondering which process is holding the file.

ps -ef will omit process with very long command line while ps -auxwww will list those process as well.

How do you find how many cpu are in your system and there details?

By looking into file /etc/cpuinfo for example you can use below command:

cat /proc/cpuinfo
What is difference between HardLink and SoftLink in UNIX?

I have discussed this Unix Command Interview questions  in my blog post difference between Soft link and Hard link in Unix


What is Zombie process in UNIX? How do you find Zombie process in UNIX?

When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in case the parent might need it – for example, the parent may need to check the child’s exit status. To be able to get this information, the parent calls ‘wait()’; In the interval between the child terminating and the parent calling ‘wait()’, the child is said to be a ‘zombie'(If you do ‘ps’, the child will have a ‘Z’ in its status field to indicate this.)

Zombie : The process is dead but have not been removed from the process table.


What is “chmod” command? What do you understand by this line “r– -w- –x?

chmod command is used to change permission of a file or directory in UNIX. The line you see shows the permission for three different set of people : user, group and others. User is the currently logged in user, while group is for all other member which are part of certain group and others means anyone other than user and group member. Each group has three permissions rwx stands for read, write and execute and they are written as user_group_others. So in above line, user has only read permission, group members has write permissions and other people has only execute permission. If it is a directory then you need execute permission to go inside that directory. See here for more detailed answer.

In a file word UNIX is appearing many times? How will you count number?

grep -c “Unix” filename

How do you set environment variable which will be accessible form sub shell?

By using export command,  for example export count=1 will be available on all sub shell.

How do you check if a particular process is listening on a particular port on remote host?

By using telnet command for example “telnet hostname port”, if it able to successfully connect then some process is listening on that port.

 

• finding host/domain name and IP address – hostname
• test network connection – ping
• getting network configuration – ifconfig
• Network connections, routing tables, interface statistics – netstat
• query DNS lookup name – nslookup
• communicate with another hostname – telnet
• outing steps that packets take to get to network host – traceroute
• view user information – finger
• checking status of destination host – telnet

http://javarevisited.blogspot.com/2010/10/basic-networking-commands-in-linuxunix.html

How do you find whether your system is 32 bit or 64 bit ?

Either by using “uname -a” command or by using “arch” command.

How do you find which processes are using a particular file?

By using lsof command in UNIX. It wills list down PID of all the process which is using a particular file.

How do you find which remote hosts are connecting to your host on a particular port say 10123?

By using netstat command execute netstat -a | grep “port” and it will list the entire host which is connected to this host on port 10123.

What is nohup in UNIX?
nohup is a special command which is used to run process in background, but it is slightly different than & which is normally used for putting a process in background. An UNIX process started with nohup will not stop even if the user who has stared log off from system. While background process started with & will stop as soon as user logoff.

What is ephemeral port in UNIX?

Ephemeral ports are port used by Operating system for client sockets. There is a specific range on which OS can open any port specified by ephemeral port range.

If one process is inserting data into your MySQL database? How will you check how many rows inserted into every second?

 

There is a file Unix_Test.txt which contains words Unix, how will you replace all Unix to UNIX?

SED command in UNIX for example you can execute following command to replace all Unix word to UNIX
sed s/Unix/UNIX/g fileName

You have a tab separated file which contains Name, Address and Phone Number, list down all Phone Number without there name and Addresses?

AWK or CUT command here. CUT use tab as default separator so you can use

cut -f3 filename

Your application home directory is full? How will you find which directory is taking how much space?

By using disk usage (DU) command in Unix for example

du –sh . | grep G  will list down all the directory which has GIGS in Size.

How do you find for how many days your Server is up?

By using uptime command in UNIX

You have an IP address in your network how will you find hostname and vice versa?

By using nslookup command in UNIX, you can read more about convert IP Address to hostname in Unix here.

1. What command can you use to display the first 3 lines of text from a file and how does it work?

There are two commands that can be used to complete this task:

        head -3 test.txt – this uses the “head” command, along with the “-3” parameter that indicates the number of lines to be displayed;

        sed ‘4,$ d’ test.txt – this command uses the Sed text editor to perform the task. If the command was simply “sed test.txt” the whole file would have been displayed; however, in our example the delete parameter was used (d) to make Sed delete everything between the 4th and the last line (defined by the $ parameter), leaving only the first 3 lines of the file. It is important to mention that Sed does not actually delete the lines from the file itself, but just from the output result.

2. How can you remove the 7th line from a file?

The easiest way is by using the following command: sed -i ‘7 d’ test.txt

Unlike the previous Sed command, this command also has the “-i” parameter, which tells Sed to make the change in the actual file.

3. What is piping?

Piping is a technique that consists of entering two or more consecutive commands, separated by the pipe symbol “|”. Once the first command is executed, its output will be used as input for the second command, the output of the second command will be used as input for the third and so on, until the whole chain of commands is executed.

4. How do you reverse a string?

You can reverse a string by using a simple piping of two commands: echo “Mary” | rev

The first command will generate the output “Mary”, which will become the input for the rev command, making it return the reverse: “yraM”.

5. How can you find out what a command does?

You use man <command-name> in order to bring up the manual page that describes the actions of the specified command and any other additional options and parameters that command might have.

These are some of the UNIX questions interviewers usually ask during basic interviews. However, if the interview is for a more technical position, you can expect to encounter questions that are more difficult; still, there is no reason to panic, as UNIX itself was created to be quite logical, so a good knowledge of the basic commands and a bit of imagination can help you get the job done.

Check out this online course to learn how to use the UNIX command line to get the most out of OS X – yes, the OS X that runs on your Mac, which is UNIX based by the way. If this doesn’t convince you that UNIX can be simple and intuitive, nothing else will.

1. How are devices represented in UNIX?

All devices are represented by files called special files that are located in /dev directory. Thus, device files and other files are named and accessed in the same way. A ‘regular file’ is just an ordinary data file in the disk. A ‘block special file’ represents a device with characteristics similar to a disk (data transfer in terms of blocks). A ‘character special file’ represents a device with characteristics similar to a keyboard (data transfer is by stream of bits in sequential order).

2. What is ‘inode’?

All UNIX files have its description stored in a structure called ‘inode’. The inode contains info about the file-size, its location, time of last access, time of last modification, permission and so on. Directories are also represented as files and have an associated inode. In addition to descriptions about the file, the inode contains pointers to the data blocks of the file. If the file is large, inode has indirect pointer to a block of pointers to additional data blocks (this further aggregates for larger files). A block is typically 8k.

Inode consists of the following fields:

  1. File owner identifier
  2. File type
  3. File access permissions
  4. File access times
  5. Number of links
  6. File size
  7. Location of the file data

3. Brief about the directory representation in UNIX.

A Unix directory is a file containing a correspondence between filenames and inodes. A directory is a special file that the kernel maintains. Only kernel modifies directories, but processes can read directories. The contents of a directory are a list of filename and inode number pairs. When new directories are created, kernel makes two entries named ‘.’ (refers to the directory itself) and ‘..’ (refers to parent directory). System call for creating directory is mkdir (pathname, mode).

4. What are the Unix system calls for I/O?

  1. open(pathname,flag,mode) – open file
  2. creat(pathname,mode) – create file
  3. close(filedes) – close an open file
  4. read(filedes,buffer,bytes) – read data from an open file
  5. write(filedes,buffer,bytes) – write data to an open file
  6. lseek(filedes,offset,from) – position an open file
  7. dup(filedes) – duplicate an existing file descriptor
  8. dup2(oldfd,newfd) – duplicate to a desired file descriptor
  9. fcntl(filedes,cmd,arg) – change properties of an open file
  10. ioctl(filedes,request,arg) – change the behaviour of an open file
  11. The difference between fcntl anf ioctl is that the former is intended for any open file, while the latter is for device-specific operations.

5. How do you change File Access Permissions?

Every file has following attributes:

  1. owner’s user ID ( 16 bit integer )
  2. owner’s group ID ( 16 bit integer )
  3. File access mode word

(r w x) – (r w x) – (r w x)
(user permission) – (group permission) – (others permission)
To change the access mode, we use chmod(filename,mode).
Example 1:
To change mode of myfile to ‘rw-rw-r–‘ (ie. read, write permission for user – read,write permission for group – only read permission for others) we give the args as:

chmod(myfile,0664) .

Each operation is represented by discrete values
‘r’ is 4
‘w’ is 2
‘x’ is 1
Therefore, for ‘rw’ the value is 6(4+2).

Example 2:
To change mode of myfile to ‘rwxr–r–‘ we give the args as:

chmod(myfile,0744).

6. What are links and symbolic links in UNIX file system?

A link is a second name (not a file) for a file. Links can be used to assign more than one name to a file, but cannot be used to assign a directory more than one name or link filenames on different computers.

Symbolic link ‘is’ a file that only contains the name of another file.Operation on the symbolic link is directed to the file pointed by the it.Both the limitations of links are eliminated in symbolic links.

Commands for linking files are:
Link “ln filename1 filename2”
Symbolic link “ln -s filename1 filename2”

7. What is a FIFO?

FIFO are otherwise called as ‘named pipes’. FIFO (first-in-first-out) is a special file which is said to be data transient. Once data is read from named pipe, it cannot be read again. Also, data can be read only in the order written. It is used in interprocess communication where a process writes to one end of the pipe (producer) and the other reads from the other end (consumer).

8. How do you create special files like named pipes and device files?

The system call mknod creates special files in the following sequence.

  1. kernel assigns new inode,
  2. sets the file type to indicate that the file is a pipe, directory or special file,
  3. If it is a device file, it makes the other entries like major, minor device numbers.

For example:
If the device is a disk, major device number refers to the disk controller and minor device number is the disk.

9. Discuss the mount and unmount system calls.

The privileged mount system call is used to attach a file system to a directory of another file system; the unmount system call detaches a file system. When you mount another file system on to your directory, you are essentially splicing one directory tree onto a branch in another directory tree. The first argument to mount call is the mount point, that is , a directory in the current file naming system. The second argument is the file system to mount to that point. When you insert a cdrom to your unix system’s drive, the file system in the cdrom automatically mounts to “/dev/cdrom” in your system.

10. How does the inode map to data block of a file?

Inode has 13 block addresses. The first 10 are direct block addresses of the first 10 data blocks in the file. The 11th address points to a one-level index block. The 12th address points to a two-level (double in-direction) index block. The 13th address points to a three-level(triple in-direction)index block. This provides a very large maximum file size with efficient access to large files, but also small files are accessed directly in one disk read.

11. What is a shell?

A shell is an interactive user interface to an operating system services that allows an user to enter commands as character strings or through a graphical user interface. The shell converts them to system calls to the OS or forks off a process to execute the command. System call results and other information from the OS are presented to the user through an interactive interface. Commonly used shells are sh,csh,ks etc.

1. There are multiple shell types of unix. What are the features of the Bourne and Korn shell?

The Bourne shell is the standard shell of unix , it provides the following fewatures:
– It gives the ability of input/output redirection.
– Allows the usage of metacharacters for file name abbreviations.
– The environment can be customized using the shell variables.
– It provides the user with a built in command set for the creation of shell programs.
– Allows the user to control a job.

The Korn shell is an extension of the Bourne shell which is backward compatible. The feature unique to the Korn shell are as follows:
– Allows the user to do command line editing.
– It maintains a command history, which enables the user to check the last commands executed.
– There is a provision for integer arithmetic.
– Provides the support for arrays and arithmetic expressions.
– Give the user the option to use aliases which is used to abbreviate the command name.

Linux test part 1 (27 questions)
Linux test part 2 (25 questions)
Linux test part 3 (25 questions)

2. What do you understand by File modes in unix?

Unix enables the user to set the desired level of privacy. It allows the user to set the access permission of the file. This is know as the file mode of a file.
The file permissions of file are displayed in the form :
drwxrwxrw filename date time
The letters r means read , w means write and x means execute. When the permission of a file is set by the user it is necessary to classify the users into three categories:
owner: the user who creates the file.
group: other users having the same permissions as the group owner.
other: all other user.
In the text drwxrwxrw the d letter signifies the directory.Following the d letter the next three letters specify the permissions of the owner. the next three specify the permissions of the group. The last three characters specify the permissions of the other users.

3. What is the alias mechanism in UNIX?

The alias command allows the user to specify or assign another name to a command. It can be also used to club together a group of commands.
For ex.
alias dir ‘ls -sFC’

The above command on being executed would tell the shell that dir is another name for the command ls -sFC. In this way the user can simply assign an alias name to a command that could be difficult to remember. Now the user simply needs to use the specified alias name as given by him in place of the command for the same effect as the code. The user can check the number of aliases active by typing in the command from the shell:
uhunix% alias pwdls ‘pwd; ls -sFC’

4. What are the different ways of redirecting I/O?

Unix allows the user to redirect the output of an operation instead of sending it to your screen. The user has to make used of the redirection pointer (> & >>). The various ways of redirecting the output are as follows:
To copy the contents of one file to another:
uhunix% cat file1 > file2
– To concatenate multiple files:
uhunix% cat file1 file2 file3 > newfile
– To append a file to another file:
uhunix% cat bottomfile >> appendeefile
– To redirect the input from instead of the keyboard to a file:
uhunix% mail username < letterfile

5. What are the use of pipes in unix?

Piping in unix allows the user to set the output of a specific operation as input to another. The piping of output from one file to another is performed by using the | symbol.
For ex.
uhunix% w | grep username

This command would allow the user to check if the specified user is logged in or not. When pipes are used in combination with redirection it gives the user the powerful ability to manipulate long operations in short steps.
For ex.
uhunix% ls | grep vi > vi.files

This command would result in the creation of a vi.files which would consist of the names of all the files in the current directory with a vi in their names.

6. What steps does the shell take after processing a command.

Once a command line is terminated by the key the shell continues to process the command line in passes.
-Parsing: The for the shell is to separate the commands line into words. For this it uses the references of spaces and delimiters specified by the user. Any consecutive spaces are replaced by a single space unless specified.
– Identifying variables: any word preceded by a $ sign are treated as variables unless specified so.
– Substitution: any command surrounded by back quotes are executed by the shell and replaces the command with the output given.
– Wild card recognition: After all the above tasks are completed the shell looks for wild card symbols in the command line.

7. What are the possible return values of kill()?

The kill() method on execution can yield the the following possible results:
– ‘0’ returned: means that the process exists with the specified PID. It will allow the user to send signals to this process.
– ‘-1’ returned, ‘errno==ESRCH’: This means that the process with the specified PID does not exist or some security enhancements is denying its existence.
– ‘-1’ returned, ‘errno==EPERM’: This means that the system would not permit the process to be killed.
The EPERM process is used to detect if a process exists or not. Any other error would specify that the process does not exist.

8. What is the super block in UNIX?

The file system of a system is described by a system block. Right at the beginning the super block is built when the file system is being created. The purpose of the super block is to contain the basic parameters of a system for ex the number of blocks and a count of the total number of files. There are two superblocks:
– The default super block is always present at a fix offset from the beginning of the system`s disk partition.
– The redundant super block is not usually referenced unless the default superblock is effected by some error or system crash.
The file system consists of files. Some files may be categorized as directories which in itself contains the pointers to files that are themselves directories or files. Every file has a descriptor known as the inode. The inode describes the ownership information of a file.

9. How can non printable characters be shown in unix?

A file sometimes may contain non printable characters such as line breaks for windows. Most of the editors used with unix do not display these non-printable characters. In case the user wants to view these characters he has to use the vim -b command to see and edit these characters. The -b flag used after vim is used to force the vim to use the binary mode.
For ex.

% cat foo.txt
Hello
World

% vim -b foo.txt
Hello^M
World^M
^M

10. What are the types of path names that can be used in unix?

In a file system which contains a hierarchy of directories the user has to specify a path to a file or directory to access it.
For ex.
/home/rohit/my/music/lyrics/text.notes
The above path refers to the file text.notes inside the folder which is inside the lyrics folder. The lyrics folder is contained by the music folder which in turn is contained by the my folder. The root directory is home.
There are tow types of paths that can be used in unix:
– Absolute Path name: This type of file name starts with the / symbol ie the root.
– Relative Path name: This path name begins from the current working directory where the user is. For the above stated path the relative directory would be:
lyrics/text.notes.

1. Write the list of commands that can used in relation to users in unix

The various commands that can be used to know about the user information in unix are as follows:
– id Show the active user id with login and group
– last Shows last logins on the system
– who Shows who is logged on the system
– groupadd admin Used to add group “admin” and user colin (Linux/Solaris)
– usermod -a -G <group> <user> Used to add existing user to group (Debian)
– usermod -A <user> <group> Used to add existing user to group (SuSE)
– adduser raj FreeBSD add user raj (interactive)

Theses commands can be executed at the shell and are distribution dependent.

12. How can file permissions be set in unix?

The command to change file permissions in unix is ‘chmod’ or also known as the change mode. With this command the user can set the permissions for users on how to access a file and what can be done with it. The syntax for changing file permissions is:
chmod [who] [+/-] [new-permissions] [file]
The who can be referred to:
u : This is for the user who owns the file.
g: This is meant to apply to the group which owns this file.
o: This is used to specify the permissions for the rest of the users.
a: This is used to specify everyone and anyone.
The +/- sign refer to if the permissions are to added or removed. The new permissions can be used in form of characters (r for read , x for execute) etc.

13. How is conditional execution be used in unix?

Conditional execution can be accomplished in unix with the use of control operators “and” and “or”. The and operator is represented as && and the syntax of their usage is :
command1 && command2
This implies that the command 2 will only be executed if the command 1 returns an exit status 0.
The or operator is represented by ||. The syntax of using the or operator is:
command1 || command2
This implies that command2 is executed if the command 1 returns a non-zero exit value.
Both the operators can be used together.
For ex.
command1 && command2 if exist status is zero || command3 if exit status is non-zero
This implies that command 2 will be executed if and only if command 1 executes or else command 3 will be executed.

14. Write a script to print numbers 5,4,3,2,1 using the while loops.

The algorithm for writing such a script would be:
– Begin: the value of i is set to 5.
– The while loop is started.
– Check of the value of i is zero. In case it is it will end the program and give the output.
– In case the i is not equal to zero the the value of i will be printed and the i`s value is decremented by 1.

The code:
i=5
while test $i != 0
do
echo “$i”
i=`expr $i – 1`
done

15. Write the algorithm and code to display the sum of all the digits of anumber?

The algorithm for the above requirement would be:
– The user is asked enter the number n.
– The sum and sd is set to zero at the beginning.
– Find the single digit in sd as n%10 which will give the left most digit.
– Construct the sum as sum = sum +sd
– The n is decremented by 1.
– If n is zero print the sum else repeat the loop.

The code for the above algorithm:
if [ $# -ne 1 ]
then
echo “Usage: $0 number”
echo ” I will find sum of all digit for given number”
echo ” For eg. $0 123, I will print 6 as sum of all digit (1+2+3)”
exit 1
fi
n=$1
sum=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo “Sum of digit for number is $sum”

16. Write the script to define a background process that will continuously print time at upper right corner of the screen.

The script to enable the user to work in the shell while defining a background program to display the time at the upper right screen is as follows:

echo
echo “Th clock”
echo “USe kill PID to dicontinue using this block”
echo “Press a key to continue. . .”

while :
do
ti=`date +”%r”`
echo -e -n “33[7s” #save current screen position & attributes

tput cup 0 69 # row 0 and column 69 is used to show clock

echo -n $ti # put clock on screen

echo -e -n “33[8u” #restore current screen position & attributes
#
#Delay fro 1 second
#
sleep 1
done

17. Write the script to display a number in reverse order? Also specify the algorithm.

The algorithm for the above program would be:
– Ask the user to enter the number.
– Set the values of rev and sd as 0.
– Find the single digit that is the left most digit.
– Construct the new number as rev * 10 + sd
– Now the n has to be decremented by 1.
– In case n is greater then zero repeat the above steps from third step or else print the number.

The code for the above algorithm is:
if [ $# -ne 1 ]
then
echo “Usage: $0 number”
echo “I will find reverse of given number”
echo “For eg. $0 123, I will print 321”
exit 1
fi

n=$1
rev=0
sd=0

while [ $n -gt 0 ]
do
sd=`expr $n % 10`
rev=`expr $rev \* 10 + $sd`
n=`expr $n / 10`
done
echo “Reverse number is $rev”

18. How can shell scripts be debugged in unix?

A user when programming in shell may face an error. The user can make use of the -v and -x option with sh or bash command to debug the shell script. The syntax format of using the option is :
sh option {shell-script-name} or
bash option {shell-script-name}

The option in the above syntax can be either -v or -x. The -v option prints the shell input lines as they are read. The -x option on the other hand expands a simple command and along with it displays the value of the ps4 variable followed by the command. It also shows the complete list of expanded arguments.

19. How can a users custom environment be made permanent?

Unix provides the user with the option to make their custom environment permanent so that the user does not have to redefine the variables of the environment again in order to define a new process. The user needs to place the set and setnv commands into certain special files to make the environment permanent. The files where the changes need to made are:
– .cshrc
– .login
– .logout
For ex the command to change the settings in the .cshrc file is:
uhunix% cat .cshrc

@(#)cshrc 1.11 89/11/29 SMI
umask 077
set notify
set history = 100
set path = ( /usr/ucb /bin /usr/bin /etc …

20. What are restricted shells ans what commands are banned in such shells?

Whenever the user runs / executes a script or a portion of it in the restricted mode it causes the shell to disable certain commands which would otherwise be available for the users. This can be considered to a security feature of unix which minimises the risk of damage while running a script. The commands or actions that are disabled during a restricted execution are as follows:
– The cd command to change the directory is restricted.
– The changing of the environment variables is not allowed.
– The user is not permitted to perform output redirection.
– To exit the restricted mode within a script is alos not allowed.
– The exec method cannot be invoked to substitute a different process for the shell.

21. What are the Unix system calls for I/O?

The following are the UNIX system calls for I/O: Open: to open a file. Syntax: open (pathname, flag, and mode)……

22. Devices 

UNIX represents all devices as files. These files are located in the directory /dev……….

23. Brief about the directory representation in UNIX

A UNIX directory is representing a file that consists of a correspondence between file names and inodes……..

24. Discuss the mount and unmount system calls

The attaching of a file system to another file system is done by using mount system call……..

25. What are the process states in Unix?

UNIX has the following process states:…….

26. What is use of sed command?

Sed command reads from a standard input and places it into the pattern space…….

What is inode

A file in UNIX is given a unique number. This unique number is known as ‘inode’…….

28. What are links and symbolic links in UNIX file system?

Link is a utility program in UNIX which establishes a hard link from one directory to another directory……..

29. Explain fork() system call.

fork() system call is used to create processes. It returns a process id. After calling fork() system call……

30. What is a zombie?

A zombie is a process which is completed the execution and still available in the process table……..

31. How do you create special files like named pipes and device files?

Special files are created by the system call ‘mknod’. Upon using the following sequence of steps, a special file will be created…….

32. How do I use poll()?

poll() allows an event to wait on a file descriptor. A pointer is accepted by poll() to a list of ‘struct pollfd’………..

 

(1) What is a UNIX shell?
The UNIX shell is a program that serves as the interface between the user and the UNIX operating system. It is not part of the kernel, but communicates directly with the kernel. The shell translates the commands you type in to a format which the computer can understand. It is essentially a command line interpreter.

Details:
Visual representation of the UNIX operating system environment:

The UNIX Operating System Environment

List of commonly used UNIX shells:

· The Bourne Shell (sh)
· The C Shell (csh or tsch)
· The Bourne Again Shell (bash)
· The Korn Shell (ksh)

(2) What needs to be done before you can run a shell script from the command line prompt?
You need to make the shell script executable using the UNIX chmod command.

Details:
This chmod command makes the shell script file “example1” executable for the user (owner) only:

$ chmod u+x example1

this syntax makes it executable for all (everyone):

$ chmod a+x example1

You can optionally use octal notation to set UNIX permissions using the chmod command (e.g., $ chmod 755 example1). This topic is beyond the scope of this article, but you can find more information by entering “unix file permissions chmod numeric notation” in your favorite search engine.

(3) How do you terminate a shell script if statement?
With fi, which is “if” spelled backwards.

Details:
The shell script example below uses an if statement to check if a file assigned to the variable myfile exists and is a regular file:

#!/bin/ksh
myfile=$1
if [ -f $myfile ]
then
echo “$myfile exists”
fi
exit 0

(See shell scripting interview question #6 below if you do not know what $1 in this example means.)

(4) What UNIX operating system command would you use to display the shell’s environment variables?
Running the “env” command will display the shell environment variables.

Details:
Sample env command output:

$ env
HISTFILE=/home/lfl/.history
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
SHELL=/bin/ksh
HOSTNAME=livefirelabs.com
USER=lfl
MAIL=/var/spool/mail/lfl
HOME=/home/lfl
HISTSIZE=1000

It would also be good to understand the purpose of the common shell environment variables that are listed in the env command output.

(5) What code would you use in a shell script to determine if a directory exists?
The UNIX test command with the -d option can be used to determine if a directory exists.

Details:
The following test command expression would be used to verify the existence of a specified directory, which is stored in the variable $mydir:

if [ -d $mydir ]
then
command(s)
fi

If the value stored in the variable mydir exists and is a directory file, the command(s) located between then and fi will be executed.

You can consult the test command’s man page (“$ man test”) to see what test command options are available for use.

(6) How do you access command line arguments from within a shell script?
Arguments passed from the command line to a shell script can be accessed within the shell script by using a $ (dollar sign) immediately followed with the argument’s numeric position on the command line.

Details:
For example, $1 would be used within a script to access the first argument passed from the command line, $2 the second, $3 the third and so on. Bonus: $0 contains the name of the script itself.

(7) How would you use AWK to extract the sixth field from a line of text containing colon (:) delimited fields that is stored in a variable called passwd_line?
echo $passwd_line | awk -F: ‘{ print $6 }’

Details:
Consider this line of text stored in the variable $passwd_line –

$ echo $passwd_line
mail:x:8:12:mail:/var/spool/mail:
$

(Background: The lines in the system passwd file are delimited (separated) by a colon (:)…$passwd_line contains a single line from the passwd file.)

The output of the echo command is piped to AWK. The -f option for the awk command informs awk of what the field separator is (colon in this example), and print $6 instructs awk to print the 6th field in the line.

(8) What does 2>&1 mean and when is it typically used?
The 2>&1 is typically used when running a command with its standard output redirected to a file. For example, consider:

command > file 2>&1

Anything that is sent to command’s standard output will be redirected to “file” in this example.

The 2 (from 2>&1) is the UNIX file descriptor used by standard error (stderr). Therefore, 2>&1 causes the shell to send anything headed to standard error to the same place messages to standard output (1) are sent…which is “file” in the above example.

To make this a little clearer, the > in between “command” and “file” in the example is equivalent to 1>.

 

(9) What are some ways to debug a shell script problem?
Although this is somewhat dependent on what the problem is, there are a few commonly used methods for debugging shell script problems.

One method, which is frequently used across all programming languages, is to insert some debug statements within the shell script to output information to help pinpoint where and why the problem is being introduced.

Another method specific to shell scripting is using “set -x” to enable debugging.

Details:
Consider the following shell script example (notice that “set -x” is commented out at this time):

#!/bin/ksh
#set -x
i=1
while [ $i -lt 6 ]
do
print “in loop iteration: $i”
((i+=1))
done
exit

This script will produce the following output:

$ ./script1
in loop iteration: 1
in loop iteration: 2
in loop iteration: 3
in loop iteration: 4
in loop iteration: 5
$

If we uncomment (remove the #) from the “set -x” line in the script, this is what is displayed when the script runs:

$ ./script1
+ i=1
+ [ 1 -lt 6 ]
+ print in loop iteration: 1
in loop iteration: 1
+ let i+=1
+ [ 2 -lt 6 ]
+ print in loop iteration: 2
in loop iteration: 2
+ let i+=1
+ [ 3 -lt 6 ]
+ print in loop iteration: 3
in loop iteration: 3
+ let i+=1
+ [ 4 -lt 6 ]
+ print in loop iteration: 4
in loop iteration: 4
+ let i+=1
+ [ 5 -lt 6 ]
+ print in loop iteration: 5
in loop iteration: 5
+ let i+=1
+ [ 6 -lt 6 ]
+ exit
$

In addition to displaying the intended output (“in loop iteration” lines), enabling debugging with “set -x” also shows each line of execution preceded by a plus sign (+).

Although this can become unwieldly for larger shell scripts, it should be obvious how useful this can be when debugging a shell script to identify the root cause of a problem.

(10) Within a UNIX shell scripting loop construct, what is the difference between the break and continue?
Using break within a shell scripting loop construct will cause the entire loop to terminate. A continue will cause the current iteration to terminate, but the loop will continue on the next iteration.

 

 

5) What is the chief difference between the –v and –x option s to set?