Pages

Showing posts with label shell-script. Show all posts
Showing posts with label shell-script. Show all posts

Thursday, December 20, 2012

Shell script: Convert decimal to ascii values

Q. I want to convert a given decimal number to ascii value, How can I do that using a shell script?

Below script will give you the ascii value corresponding number

#!/bin/bash

printf "Please enter a value between 32 to 127(Range of ascii values): "
read VAL1
printf "\\$(printf "%o" $VAL1)\n"

Let me explain this script. The script will take input from user in to VAL1 variable and pass it to printf command. printf "%o" $VAL1 will try to change the decimal value to octal. then this octal value is once again feed to the second printf as printf "\octvalue\n". printf command have one option what ever followed after \ will be treated as octal which helps use to convert to ascii values.   Want to print all the ascii values? Execute below script to do that   #!/bin/bash
for i in {32..127}
do
printf "\\$(printf "%o" $i)\n"
done

or just one single liner which will work in bash version 4 and above.    printf  "$(printf "\%o\n" {32..127})"   Please comment your thoughts on this.

Monday, December 17, 2012

Shell script to convert Octal to Decimal

This is a small script to convert octal number to decimal numbers.  We already shown how to do hex2dec conversion in our previous post.  In this post too we will use (()) and bc commands to accomplish our task.

Below we will see number of ways to do this conversion using .

Method1: Use (()) brace expatiation. 

#!/bin/bash
read -p "Please enter OCT number: " OCT1
echo "The decimal value of $HEX1 is $((8#$HEX1))"

Save above file as oct2dec.sh

Change permissions to this script now and execute as follows

chmod +x hex2dec.sh

Executing shell script./hex2dec.sh

Method2: Using bc command

#!/bin/bash
read -p "Please enter OCT number: " OCT1

echo "ibase=8; $OCT1" | bc


Save above file as oct2dec.sh

Change permissions to this script now and execute as follows

chmod +x oct2dec.sh

Executing shell script

./oct2dec.sh
Please share your thoughts on this.

Sunday, December 16, 2012

Shell script to convert HEX to decimal number

This is a small script to convert hex number to decimal numbers. Here we can use different machination to do this. One is to use inbuilt (()) braces to do this. Hexadecimal numbers have base as 16. So if I consider hex value of 13, it will be equal to 1*16+3=19 decimal value.

Below we will see number of ways to do this conversion using .

Method1: Use (()) brace expatiation. 

#!/bin/bash
read -p "Please enter HEX number: " HEX1
echo "The decimal value of $HEX1 is $((16#$HEX1))"

Save above file as hex2dec.sh

Change permissions to this script now and execute as follows

chmod +x hex2dec.sh

./hex2dec.sh

Method2: Using bc command

#!/bin/bash
read -p "Please enter HEX number: " HEX1

echo "ibase=16; $HEX1" | bc


Save above file as hex2dec.sh

Change permissions to this script now and execute as follows

chmod +x hex2dec.sh

./hex2dec.sh



Thursday, December 13, 2012

bash variable assignment "command not found" error

Q. I am getting "command not found" error when doing dynimic variable assignemet in Shell scripts. How can I resolve this issue?

Let use replicate this issue.

#!/bin/bash
VAR1=abc
i=0
VAR$i=$VAR1
echo "Value of VAR0 is $VAR0"


Save this file and execute this script.
bash abc.sh


abc.sh: line 4: VAR0=abc: command not found


Value of VAR0 is


If you see I am getting command not found error.

This is due to the line VAR$i=$VAR1, in which we are try to substitue two variable at a time, one on RHS and other on LHS. This is not possible by default in Shell scripting. First we have to substitue "i" value then substitue $VAR1 value. To resolve this issue we have to use eval command which gives us second chance to execute a command.

Modified version of above script as below.

#!/bin/bash
VAR1=abc
i=0
eval VAR$i=$VAR1
echo "VAlue of VAR0 is $VAR0"


Output:

bash abc.sh

VAlue of VAR0 is abc

Hope this helps to resolve dynamic assignmet of variables.



 

Saturday, August 18, 2012

Move all file description(first 5 lines) of a file to file

Q. I have a requirement to move all my shell script meta data such as interpreter, Author, date, purpose etc which occupies my first 5 lines.

With head

head -n5 *.sh > file.txt

With SED


sed -n '1,4 p' xyz.txt 

or

sed '5,$ d' xyz.txt

you can keep this one in for loop and execute sed command get your file content to other file.

Tuesday, August 14, 2012

Linux/Unix bash Shell script for user account expiry notification


Shell script to display user account expiry details

Q.Can we write a shell script to send notification to administrator or root about the accounts which are going to expire?
Ans : Here is a small script which will give you mail on the accounts which are expired and which are going to expire in 7 days. You are free to use and modify this script for your work.


#!/bin/bash
#Author:Surendra Kumar Anne
#Created on:09-02-2012
#Purpose:To check the user account expire status in Linux, unix, BSD etc

cat /etc/shadow | cut -d: -f1,8 | sed /:$/d > /tmp/expirelist.txt
totalaccounts=`cat /tmp/expirelist.txt | wc -l`
for((i=1; i<=$totalaccounts; i++ ))
       do
       tuserval=`head -n $i /tmp/expirelist.txt | tail -n 1`
       username=`echo $tuserval | cut -f1 -d:`
       userexp=`echo $tuserval | cut -f2 -d:`
       userexpireinseconds=$(( $userexp * 86400 ))
       todaystime=`date +%s`
       #check if the user expired or not?
       if [ $userexpireinseconds -ge $todaystime ] ;
           then
           timeto7days=$(( $todaystime + 604800 ))
                if [ $userexpireinseconds -le $timeto7days ];
                then
                mail -s "The account $username will expire less than 7 days" root
                fi
       else
       mail -s "The user account $username already expired" root
       fi
done

This script will send multiple mails to root about the status of expired and going to expire user accounts.

Linux/Unix Bash shell script to zip entire folder

Write a Linux shell script to zip entire folder.

This is a small script which will zip a folder to any zipping algorithms such as zip, gz, bz2 etc.


#!/bin/bash
read -p "please enter the folder location: " SFOLD1
read -p "Target location to save this zip file: " DFOLD1
read -p "Select compress type(zip, tar.gz, tar.bz2): " COMP1
case $COMP1 in
zip)zip ${DFOLD1}/file1.zip $SFOLD1;;
tar.gz) tar cvfz ${DFOLD1}/file1.tar.gz $SFOLD1;;
tar.bz2) tar cvfj ${DFOLD1}/file1.tar.bz2 $SFOLD1;;
*) echo "Please enter a valid compression(zip, tar.gz, tar.bz2)";exit;;
esac

Let me explain what this script will do. This script will ask for source folder which you want to compress, destination folder where you want to create this compressed folder and the type of compress.

Monday, August 13, 2012

Linux/Unix shell script to number the lines of a file

Bash Shell script list file content along line numbers


How can I display line numbers of a file. This can be achieved in Linux and Unix using many commands such as cat, nl, sed etc. In this post we will see how to write a script to display line numbers as well as with commands

Example1: Display line numbers using cat

cat -n filename.txt

Example2: Save a file with line numbers included in the file

cat -n filename.txt > file2.txt

Example3: Display line number of a file along the content using nl command
nl filename.txt

Example4: How can I count number of lines in a file?

cat filename.txt | wc -l

Example5: Display line numbers using sed command.
sed = tem.txt | sed ‘N;s/\n/\t/’

Through Shell script


#!/bin/bash
j=0
for i in `cat filename.txt`
do
let j+=1
echo "$j $i"
done

Please feel free to add your own way of listing file content along with line numbers.