Ans. ./script argument
Example : Script will show filename
./show.sh file1.txt
cat show.sh
#!/bin/bash
cat $1
Ans. First argument: $1,
Second argument : $2
Example : Script will copy file (arg1) to destination (arg2)
./copy.sh file1.txt /tmp/
cat copy.sh
#!/bin/bash
cp $1 $2
Ans. $#
Ans. $0
Ans. $?
Ans. tail -1
Ans. head -1
Ans. Add -xv to #!/bin/bash
Example
#!/bin/bash –xv
Ans. function example {
echo "Hello world!"
}
Ans. V1="Hello"
V2="World"
let V3=$V1+$V2
echo $V3
Output
Hello+World
Ans. V1=1
V2=2
V3=$V1+$V2
echo $V3
Output
3
Remember you need to add "let" to line V3=$V1+$V2
then echo $V3 will give 3
if without let , then it will be
echo $V3 will give 1+2
Ans. if [ -f /var/log/messages ]
then
echo "File exists"
fi
Ans. for loop :
for i in $( ls ); do
echo item: $i
done
while loop :
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
until loop :
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
Ans. That line tells which shell to use. #!/bin/bash script to execute using /bin/bash. In case of python script there there will be #!/usr/bin/python
Ans. head -10 file|tail -1
Ans. #
Ans. 0
Ans. Makes variable public in subshells
Ans. add "&" to the end of script
Ans. Makes script executable for script owner
Ans. Redirects output stream to file or another stream.
Ans. & - we using it when want to put script to background
&& - when we wand to execute command/script if first script was finished successfully
Ans. When we need to run several commands if condition meets.
Ans. My name is $name
Ans. #
Ans. variable
Ans. ' - we use it when do not want to evaluate variables to the values
" - all variables will be evaluated and its values will be assigned instead.
Ans. Add "exec >log.txt 2>&1" as the first command in the script
Ans. echo ${variable:x:y}
x - start position
y - length
example:
variable="My name is Petras, and I am developer."
echo ${variable:11:6} # will display Petras
Ans. echo ${variable#*:*:*:}
or
echo ${variable##*:}
Ans. echo ${variable%:*:*:*}
or
echo ${variable%%:*}
Ans. awk -F: '$3<100' /etc/passwd
Ans. cat /etc/passwd|cut -d: -f4|sort|uniq -c|while read c g
do
{ echo $c; grep :$g: /etc/group|cut -d: -f1;}|xargs -n 2
done
Ans. IFS=":"
Ans. $