Q41:-Which one is correct "export $variable" or "export variable" ?
Ans:- export variable=value
Q42:-How to list files where second letter is a or b ?
Ans:- ls -d ?[ab]*
Q43:-How to add integers a to b and assign to c ?
Ans:-
c=$((a+b))
or
c=`expr $a + $b`
or
c=`echo "$a+$b"|bc`
Q44:-How to remove all spaces from the string ?
Ans:-echo $string|tr -d " "
Q45:-Rewrite the command to print the sentence and converting variable to plural: item="car"; echo "I like $item" ?
Ans:- item="car"; echo "I like ${item}s"
Q46:-Write the command which will print numbers from 0 to 100 and display every third (0 3 6 9 …) ?
Ans:-
for i in {0..100..3}; do echo $i; done
or
for (( i=0; i<=100; i=i+3 )); do echo "Welcome $i times"; done
Q47:-What difference between [ $a == $b ] and [ $a -eq $b ]
Ans:-
[ $a == $b ] - should be used for string comparison
[ $a -eq $b ] - should be used for number tests
Q48:-What difference between = and == ?
Ans:-
"=" - we using to assign value to variable
"==" - we using for string comparison
Q49:-Write the command to test if $a greater than 12 ?
Ans:- [ $a -gt 12 ]
gt - Greater Than
ge - Greater Than or Equal to
lt - Less Than
le - Less Than or equal to
eq - Equal to
Q50:-How to check if string begins with "abc" letters ?
Ans:-[[ $string == abc* ]]
Q51:-What difference between [[ $string == abc* ]] and [[ $string == "abc*" ]]
Ans:-
[[ $string == abc* ]] - will check if string begins with abc letters
[[ $string == "abc*" ]] - will check if string is equal exactly to abc*
Q52:-How to list usernames which starts with ab or xy ?
Ans:- egrep "^ab|^xy" /etc/passwd|cut -d: -f1
Q53:- What difference between $* and $@
Ans:-
$* - Print all passed arguments to the script as a single string
$@ - Print all passed arguments to the script as delimited list. Delimiter $IFS
$0 - Print the Script Name
$! - Most recent background command PID
$? - Most recent foreground process exit status 0 if success else any arbitiry number.
$$ - Give the PID of current shell
$# - Give the number of passed arguments
Q54:-How to define array in bash ?
Ans:- array=("Hi" "my" "name" "is")
Q55:-How to print the first array element ?
Ans:- echo ${array[0]}
echo ${array[0]} - Print first element of array
echo ${array[@]} - Print all elements of array
echo ${!array[@]} - Print all indexes of array
Q56:- How to remove array element with id 2 ?
Ans:- unset array[2]
Q57:-How to add new array element with id 8 ?
Ans:- array[8]="New_element"
Q58:-How shell script get input values ?
Ans:- There are 2 ways to pass value to shell script
a) via parameters
./script param1 param2
b) via read command
read -p "Destination backup Server : " desthost
Q59:-How can we use "expect" command in a script ?
Ans:-
/usr/bin/expect << EOD
spawn rsync -ar ${line} ${desthost}:${destpath}
expect "*?assword:*"
send "${password}\r"
expect eof
EOD
No comments:
Post a Comment