Showing posts with label Shell Script. Show all posts
Showing posts with label Shell Script. Show all posts

Wednesday 30 August 2017

Unix Shell Scripting Tutorial - page 4

Unix Shell Scripting Tutorial - page 4

Question: What are man pages?
Unix's version of Help files are called man pages.


Question: Can we perform regular expression in unix?
Yes, We can do.
We can do using sed.
Full form of SED is stream editor.
For Example(s)
cat /etc/passwd | sed -n '1,3p'

REGEX_DATE='^\d{2}[/-]\d{2}[/-]\d{4}$'
echo "$1" | grep -P -q $REGEX_DATE
echo $?



Question: What are character meaning in regular expression?
  1. ^a: Start with a
  2. a$: End with a
  3. .: Match any single character.
  4. *: Match zero OR more occurances.
  5. [abcd]: Any character from a,b,c and.
  6. \: Skip the effects of special character.
  7. [a-z]: Match a-z any character.
  8. [a-z0-9]: March a-z OR 0-9 any character.
  9. [[:alnum:]]: Alphanumeric [a-z A-Z 0-9]
  10. [[:alpha:]]: Alphabetic [a-z A-Z]
  11. [[:blank:]]: Blank characters (spaces or tabs)
  12. [[:cntrl:]]: Control characters
  13. [[:digit:]]: Numbers [0-9]
  14. [[:graph:]]: Any visible characters (excludes whitespace)
  15. [[:lower:]]: Lowercase letters [a-z]
  16. [[:print:]]: Printable characters (non-control characters)
  17. [[:space:]]: Whitespace
  18. [[:punct:]]: Punctuation characters
  19. [[:upper:]]: Uppercase letters [A-Z]
  20. [[:xdigit:]]: Hex digits [0-9 a-f A-F]



Question: Can we execute multiple command in single statement?
Yes, We can execute multiple commands in single statement.
For Example
sed -e 'command1' -e 'command2'  -e 'command3'  -e 'command4' 



Question: What are the most comman commands?
  1. cat filename: Displays a filename.
  2. cd dirname: Moves you to the identified directory.
  3. cp file1 file2: Copies one file/directory to the specified location.
  4. file filename: Identifies the file type
  5. head filename: Shows the beginning of a file
  6. less filename:
  7. ls dirname: Display the content of directory.
  8. mv file1 file2: Rename a file
  9. tail filename: Display the end of file
  10. touch filename: Create a blank file.
  11. whereis filename: Display the location of file
  12. :
Question: What is use of df command?
Its used to display the disk space usages.
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/xvda1      20509288 15036052   5372988  74% /
devtmpfs         2015996       64   2015932   1% /dev
tmpfs            2024996        0   2024996   0% /dev/shm
/dev/xvdf      103081248 44579524  53258844  46% /var/lib/mysql
/dev/xvdg      103081248 89393356   8445012  92% /mnt



Question: What is use of du command?
Its used to display how much space is taking by each directory.


Question: What mount command used for?
mount point is a directory to access your data (files/folders) which is stored in your disks.


Question: What unmount command used for?
To unmount (remove) the file system from your system.
For Example
umount /dev/cdrom



Question: What are the different type of users in unix?
  1. Root User: It is super admin who have all the access and no need to any further permission.
  2. System accounts: It have access of system-specific components like mail accounts and the sshd accounts.
  3. User accounts: User accounts provide interactive access to the system. Users are typically assigned to these accounts and usually have limited access files and directories.



Question: What are the main administrator files?
  1. /etc/passwd: Keeps the user account and password info.
  2. /etc/shadow: Holds the encrypted password.
  3. /etc/group: It have group information.
  4. /etc/gshadow: secure group account information



Monday 28 August 2017

Unix Shell Scripting Tutorial - page 3

Unix Shell Scripting Tutorial - page 3

Question: What are special variable in unix?
Special variable are those variable which are reserved by unix shell. For Example $$ is special variable and it return the current process id.


Question: How to use array variable in for loop?
Yes, we can define.
NUMS="1 2 3 4 5 6 7 8 9"
for NUM in $NUMS
do
   Q=`expr $NUM % 2`
   if [ $Q -eq 0 ]
   then
      echo "Number is an even number!!"
      continue
   fi
   echo "Found odd number"
done



Question: Give example of while with break?
a=2
while [ $a -lt 10 ]
do
   echo $a
   if [ $a -eq 5 ]
   then
      break
   fi
   a=`expr $a + 1`
done

Once $a will reach to 5, then it will break the statement.
output
2
3
4
5



Question: How to print the special character as string?
Use the Backslash before the special character, It will print. For Example:
echo \$;



Question: What is Output Redirection?
We use > notation to Output Redirection.
who > users
If a command has its output redirected to a file and the file already contains some data, that data will be lost.

We can use >> to append in file, it will not lost the data.
who >> users


Question: What are the Redirection Commands?
  1. pgm > file: Output of pgm is redirected to file.
  2. pgm < file: Program pgm reads its input from file.
  3. pgm >> file: Output of pgm is appended to file.
  4. n > file: Output from stream with descriptor n redirected to file
  5. n >> file: Output from stream with descriptor n appended to file
  6. n >& m: Merges output from stream n with stream m.



Question: How to create a custom function?
# Create a new function with name of Hello
Hello () {
   echo "Hello World! How are you?"
}

# Call the function
Hello



Question: How to create a custom function with parameter?
# Define your function here
Hello () {
   echo "Hello World! How are you?"
   echo "First parameter: $1";
   echo "Second parameter: $2";    
}

# Invoke your function
Hello first second

Output
Hello World! How are you?
First parameter: first
Second parameter: second



Thursday 24 August 2017

Unix Shell Scripting Tutorial - page 2

Unix Shell Scripting Tutorial - page 2

Question: What are special variable in unix?
Special variable are those variable which are reserved by unix shell. For Example $$ is special variable and it return the current process id.


Question: List the special variable?
  1. $$: Give the current process ID.
  2. $0: File name of current script.
  3. $n: $1 will give first argument, $2 will give 2nd argument.
  4. $#: The number of arguments supplied to a script.
  5. $*: Return all the arguments.
  6. $?: The exit status of the last command executed.
  7. $!: Process number of last command



Question: Can we define array variables?
Yes, we can define.
NAME01="Name 1"
NAME02="Name 2"
NAME03="Name 3"
NAME04="Name 4"
NAME05="Name 5"



Question: How to define array variable and use them?
Define Array variable
NAME01="Name 1"
NAME02="Name 2"
NAME03="Name 3"
NAME04="Name 4"
NAME05="Name 5"

Use single variable
echo $NAME[2];


Print each and every variable
for i in NAME01 NAME02 NAME03 NAME04 NAME05
do
  echo $i;
done



Question: How to print array in single line?
echo  ${NAME[*]};

Question: How to use Relational Operators?
  1. -eq: Equal
  2. -ne: Not Equal
  3. -gt: Greater than
  4. -lt: Less than
  5. -ge: Greater than OR Equal to
  6. -le: Less than OR Equal to



Question: How to use Boolean Operators?
  1. !: Not
  2. -o: OR
  3. -a: And



Question: Give an example of while?
a=1
while [ $a -lt 10 ]
do
   echo $a
   if [ $a -eq 5 ]
   then
      break
   fi
   a=`expr $a + 1`
done



Question: What is Substitution?
The shell performs substitution when it encounters an expression that contains one or more special characters.


Question: Give few example of Variable Substitution?
Variable substitution enables the shell programmer to manipulate the value of a variable based on its state.
Examples
${var}

${var:-word}
If var is null, word is substituted for var.
${var:=word}
If var is null, var is set to the value of word.
${var:?message}
If var is null, message is printed to standard error.
${var:+word}
If var is set, word is substituted for var.


Question: What are the Metacharacters?
metacharacters have special meaning while using them in any Shell Script and causes termination of a word unless quoted.


Following are the meta characters.
    
        * ? [ ] ' " \ $ ;  ( ) | ^ 
    



Wednesday 23 August 2017

Unix Shell Scripting Tutorial - page 1

Unix Shell Scripting Tutorial - Step 1

Question: How to create file shell script file?
echo "Hello World? How are you all?";



Question: How to make a file executable?
chmod a+rx script1.sh



Question: How to run execute file?
Just with name, you can execute.
script1.sh



Question: How to add comment in shell script files?
Use hash(#) before the line for comment.
chmod a+rx script1.sh



Question: How to use variable in shell script file?
name="Manoj"
echo "My Name is : $name"



Question: What is variable convention?
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).


Question: How to define read only variable?
readonly version="10.2"
echo "Version is : $version"



Question: How to unset variable?
name="Manoj"
unset name;



Question: What are different type of variable?
  1. Local Variables: A local variable is a variable that is present within the current instance of the shell.
  2. Environment Variables: An environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly.
  3. Shell Variables: A shell variable is a special variable that is set by the shell.



Question: How to use Escape Characters?
Use slash \ for Escape the character;

echo "Hello \"User\""; // Hello "User"
echo "Slash \\" ;// Slash \



Question: How to use expression in shell script file?
Use expr enclosed with caret (~), For Example
myvar=10;
result=`expr 10 * 5`
echo "Multiple: $result";

myvar=10;
result=`expr 10 + 5`
echo "Additon: $result";

myvar=10;
result=`expr 10 -5`
echo "Subtract: $result";

Question: How to use loop?
for i in 1 2 3 4 5
do
  echo "Number $i"
done



Question: How to use if else condition?
if [ 2 = 3 ]
then
  echo "Both are equal"
else
  echo "Both are not equal"
fi



Question: How to use if else elseif condition?
if [ 2 = 3 ]
then
  echo "Both are equal"
elseif [ 2 > 3 ]
  echo "2 is greater than 3"
  else
  echo "2 is less than 3"
fi



Question: How to read from user?
echo "What is your name?"
read name
echo "Your name is $name"


Question: How to search a folder with name?
find ~ -type d -name "*foldername*" -print