Wednesday, June 27, 2012

Basic shell scripting part 3


1. What is Shell Scripting ?
        Shell scripting, in Linux or Unix,  is programming with the shell using which you can automate your tasks. A shell is the command interpreter which is the interface between the User and the kernel. A shell script allows you to submit a set of commands to the kernel in a batch. In addition, the shell itself is very powerful with many properties on its own, be it for string manipulation or some basic programming stuff. 

2. The command "cat file" gives error message "--bash: cat: Command not found". Why?
    It is because the PATH variable is corrupt or not set appropriately. And hence the error because the cat command is not available in the directories present PATH variable.

3. How to find the length of a string  in Linux?

$ x="welcome"
$ echo ${#x}
7
4. What are the different timestamps associated with a file?
  • Modification time:- Refers to the time when the file is last modified.
  • Access time :- The time when the file is last accessed.
  • Changed time :- The time when the attributes of the file are last changed.
5. How to get the list of files alone in a directory in Linux?
$ ls -lrt | grep ^-
6. How to find the last modified file or the newest file in a directory?
$ ls -lrt | grep ^- | awk 'END{print $NF}'
7. How to access the 10th command line argument in a shell script in Linux?
     $1 for 1st argument, $2 for 2nd, etc... For 10th argument, ${10}, for 11th, ${11} and so on.

8. How to find the sum of all numbers in a file in Linux?
$ awk '{x+=$0}END{print x}' file
9. How to delete a file which has some hidden characters in the file name?
       Since the rm command may not be able to delete it, the easiest way to delete a file with some hidden characters in its name is to delete it with the find command using the inode number of the file.

$ ls -li
total 32
9962571 -rw-r--r-- 1 guru users 0 Apr 23 11:35
$ find . -inum 9962571 -exec rm '{}' \;
10. Using the grep command, how can you display or print the entire file contents?
$ grep '.*' file
11. What is the difference between a local variable and environment variable in Linux?
         A local variable is the one in which the scope of the variable is only in the shell in which it is defined. An environment variable has scope in all the shells invoked by the shell in which it is defined.

12. What does the 'execute' permission in a directory stand for?
       Without the execute permission on a directory, the user will not be able to traverse or in other words, do a "cd" to the directory.

13. How to find the total number of arguments in a shell script in Linux?
      The shell special variable, $# ,contains the total number of arguments passed to a shell script.

14.  How to remove the Control-M character from a file in Linux?

$ dos2unix file
15. In which file should a variable be set  in order to make the setting permanent?
      The variable should be set in the profile file to make the setting permanent. The appropriate profile depends on the default shell being set for the user.

16. What is a she-bang line in a shell script?
          She-bang line in a shell script is the first line, if present. It starts with '#!' and followed up with a full path of a shell. The shell specified indicates the shell in which this script will be run. The entry of she-bang is not mandatory, however, if present, should be the first line of the script. With a number of shells available and syntax being specific for a given shell, it is always good to specify the she-bang line in a shell script.

17. A file contains many lines, and each line containing multiple words. How to find out the unique words and the word count of each of the words?
$ cat file
apple orange
banana apple orange
papaya
$ awk '{for(i=1;i<=NF;i++)a[$i]++;}END{for(i in a){print i, a[i];}}' file
banana 1
apple 2
orange 2
papaya 1
18. What is an internal command in Linux?
        Internal commands are also called shell built-in commands. Example: cd,fg. Since these are shell built-in, no process is created while executing these commands, and hence are considered to be much faster.

19. x and y are two variables containing numbers? How to add these 2 numbers?

$ expr $x + $y
20.  How to add a header record to a file in Linux?

$ sed -i '1i HEADER' file
21. How to find the list of files modified in the last 30 mins in Linux?

$ find . -mmin -30
22. How to find the list of files modified in the last 20 days?

$ find . -mtime -20
23. How to find the files modified exactly before 30minutes?

$ find . -mmin 30

24. A string contains a absolute path of a file. How to extract the filename alone from the absolute path in Linux?

$ x="/home/guru/temp/f1.txt"
$ echo $x | sed 's^.*/^^'
25. How to find all the files created after a pre-defined date time, say after 10th April 10AM?
       This can be achieved in 2 steps:
       1. Create a dummy file with the time stamp, 10th April 10AM.
       2. Find all the files created after this dummy file.

$ touch -t 1004101000 file
$ find . -newer file
26. How to print the contents of a file line by line in Linux?

$ while read line
> do
>   echo $line
> done < file
27. The word "Unix" is present in many .txt files which is present across many files and also files present in sub directories. How to get the total count of the word "Unix" from all the .txt files?
$ find  . -name *.txt -exec grep -c Unix '{}' \; | awk '{x+=$0;}END{print x}'
28.  How to get tomorrow's date in Linux?
$ date -d "1 day"
29. How to join all lines in a file using comma?
$ paste -s -d, file
30. How to join all lines in a file without any delimiter?
$ paste -s --delimiter="" file
31. How to join every 2 lines in a file in Linux?
$ sed 'N;s/\n//' file
32. A shell script will ask for 3 inputs. The user will not be physically present to be able to give it manually. How can the script be run without having to manually give the input?
    Put those 3 input values in a file, and make the script to read this file as input. For example:
     Assume the 3 values to be : 3, 10 and 20:
$ cat file
3
10
20
  and assuming the script is hello.sh, run it like:
./hello.sh < file
33.  How to find the total number of a lines in a file in Linux?
$ wc -l file | awk '{print $1}'


34. How to print the first line or the header record in a file?
$ head -1 file

35. How to  replace all occurrences of "Unix" to "Linux" in a file?

$ sed 's/Unix/Linux/g' file
36. How to make the above changes permanent in the file?

$ sed -i 's/Unix/Linux/g' file
37. How to  replace only the first occurrence of "Unix" to "Linux" in a string in Linux?

$ sed 's/Unix/Linux/' file
38. How to  replace only the second occurrence of "Unix" to "Linux" in a string in Linux?

$ sed 's/Unix/Linux/2' file
    In fact, to replace nth occurrence of a string in a file, it is:

$ sed 's/Unix/Linux/n' file  #where n is the nth occurrence

39. How to add leading zeros to every line in a file in Linux?

$ sed 's/^/0000/' file
40. How to add trailing zeros to  every line in a file in Linux?

$ sed 's/$/00/' file

41. How to get yesterday's date in Linux?

$ date -d "1 day ago"
42. I have a file with SQL commands. How can I open a sqlplus session in Linux and run this SQL file?

$ sqlplus guru/unix11@XE @file.txt
    where file.txt is the ASCII file containing the sql instructions.

43. The ps command will disclose the sqlplus connect string if any sqlplus session is ON. How to prevent the sqlplus connect string from appearing in the ps command in Linux?
     While connecting to sqlplus, instead of connecting in the normal way, connect as below:

$ sqlplus /nolog
> connect guru/unix11@XE
44. How to rename a group of files from .txt to .exe in Linux?

for i in *.txt
do
  x=`basename $i .txt`
  mv $i $x.exe
done
45. After logging in to your account in Linux, you did "cd log".  There was no "log" directory under the current directory, still the "cd" command traversed to a log directory under a different location? How it happened?
     It is because the CDPATH variable is set.

46. How to zero pad a number in Linux?
      Say, to zero pad a number to 4 places:

$ x=20
$ printf "%04d\n" $x
46. How to find all the .c and .h files in Linux?

$ find . -name "*.[ch]"
47. How to find the list of all the .c files and display only the file name, instead of the default find output which includes the relative path?

$ find . -name *.c | sed 's^.*/^^' 
48. How to copy a file with the same time stamp as the source file in Linux?

$ cp --preserve=timestamp file1 file2
49. How to copy a file "file1" to "file2" by passing only one argument to cp command?

$ cp file{1,2}
50. What is the difference between the source command and dot(.) command in Linux?
       No difference. Both are used for sourcing a file in Linux. bash, csh and some more shells using source command, whereas ksh uses dot(.) command to source a file.