Thursday, November 17, 2022

Some bash keyboard shortcuts and special characters

Some bash keyboard shortcuts


  • Esc + T: Swap the last two words before the cursor
  • Ctrl + H: Delete the letter starting at the cursor
  • Ctrl + W: Delete the word starting at the cursor
  • TAB: Auto-complete files, directory, command names and much more
  • Ctrl + R: To see the command history.
  • Ctrl + U: Clear the line
  • Ctrl + C: Cancel currently running commands.
  • Ctrl + L: Clear the screen
  • Ctrl + T: Swap the last two characters before the cursor

Some bash special characters

Characters Description
/ Directory separator, used to separate a string of directory names. Example: /home/projects/file
\ Escape character. If you want to reference a special character, you must “escape” it with a backslash first. Example: \n means newline; \v means vertical tab; \r means return
# Lines starting with # will not be executed. These lines are comments
. Current directory. When its the first character in a filename, it can also “hide” files
.. Returns the parent directory
~ Returns user’s home directory
~+ Returns the current working directory. It corresponds to the $PWD internal variable
~- Returns the previous working directory. It corresponds to the $OLDPWD internal variable
* Represents 0 or more characters in a filename, or by itself, it matches all files in a directory. Example: file*2019 can return: file2019, file_comp2019, fileMay2019
[] Can be used to represent a range of values, e.g. [0-9], [A-Z], etc. Example: file[3-5].txt represents file3.txt, file4.txt, file5.txt
| Known as “pipe". It redirects the output of the previous command into the input of the next command. Example: ls | less
< It redirects a file as an input to a program. Example: more < file.txt
> In script name >filename it will redirect the output of “script name” to “file filename”. Overwrite filename if it already exists. Example: ls > file.txt
>> Redirect and append the output of the command to the end of the file. Example: echo "To the end of file" >> file.txt
& Execute a job in the background and immediately get your shell back. Example: sleep 10 &
&& “AND logical operator”. It returns (success) only if both the linked test conditions are true. It would run the second command only if the first one ran without errors. Example: let "num = (( 0 && 1 ))"; cd/comp/projs && less messages
; “Command separator”. Allows you to execute multiple commands in a single line. Example: cd/comp/projs ; less messages
? This character serves as a single character in a filename. Example: file?.txt can represent file1.txt, file2.txt, file3.txt

 

Wednesday, November 16, 2022

Some loop examples in bash scripting for and while

 
for i in  1 2 3 4 5
do
echo "Maharshi"
done

for i in  {0..10..5}
do
echo -e "Maharshi\t$i"
done

for i in  {10..0..-5}
do
echo -e "Maharshi\t$i"
done

for i in {1..10}
do
printf "%s\n%s\n" "Galgotias" "UNiversity";
done


for i in $(seq 1 10)
do
printf "%s\n%s\n" "Galgotias" "UNiversity";
done


for i in $(seq 1 2 10)
do
printf "i is %d\n" "$i";
done

for i in $(seq 10 -2 1)
do
printf "i is %d\n" "$i";
done

for((i=4;i<10;i++))
do
echo "Maharshi";
done

echo "enter a number";
read number;
if (((number < 10))  &&  ((number > 1)))||(((number<100))&&((number>90)))
then
echo $number;
fi

i=0
while ((i < 5 ))
do
echo "maharshi";
i=$(($i+1));
done

arithmatic expansion

i=15;
i=$(($i*5));
echo $i;

Digonal number pattern in bash solved

  printf "Enter row number";
    read n;
    c=1
    for((i=1;i<=(n-1);i++))
    {
   
        for((j=1;j<=(n-1);j++))
        {
            if(((i==j)||(j==(n-i))))
            then
            printf $c ;
            else
            printf " ";
            fi
        }
        if ((c>=4))
        then
        ((c=c-1));
        else
        ((c=c+1));
        fi
        printf "\n";
    }

 

 


Monday, November 14, 2022

Set up cgi-perl in Debian 11

apt-get install apache2

/sbin/a2enmod cgid


systemctl restart apache2


cd /usr/lib/cgi-bin


nano /usr/lib/cgi-bin/test.pl


#!/usr/bin/perl

print "Content-Type: text/html\n\n";

print ("<h1>Perl is working!</h1>");

chmod 755 /usr/lib/cgi-bin/test.pl

http://172.31.8.195/cgi-bin/test.pl


Source: https://techexpert.tips/apache/perl-cgi-apache/

Some bash shell script examples for beginners

 even odd


echo "Please enter a number"
read num
if [ `expr $num % 2`  == 0 ]
then
echo "$num is even"
else
echo "$num is odd"
fi


largest of three numbers

echo "please enter 3 numbers"
read num1
read num2
read num3

if test $num1 -gt $num2 -a $num1 -gt $num3
then
echo "$num1 is the greatest"
elif test $num2 -gt $num3
then
echo "$num2 is the greatest"
else
echo "$num3 is the greatest"
fi


calculate net salary


echo "enter basic salary"
read bs
hra=`echo $bs*10/100 | bc`
ta=`echo $bs*15/100 | bc`
da=`echo $bs*2/100 | bc`
pf=`echo $bs*10/100 | bc`
tax=`echo $bs*5/100 | bc`

netsal=`echo $bs+$hra+$ta+$da-$pf-$tax | bc`
echo "net salary is $netsal"


calculate conditional discount and tax


echo  "please enter the amount that you want to buy"
read amount
if test  $amount -lt 1000
then
tax=`echo $amount \* 2/100 | bc`
discount=`echo $amount \* 10/100 | bc`
else
tax=`echo $amount \* 5/100 | bc`
discount=`echo $amount \* 20/100 | bc`
fi

totalcost=`echo $amount - $tax + $discount | bc`
echo  "total cost is $totalcost"