Sunday 30 October 2016

script to print fibonacci series

echo "Enter n for Fibonacci series: - "
read n
echo "Fibonacci series is: - "
echo "0"
echo "1"
i=0
j=1
cnt=2
while [ $cnt -le $n ]
do
k=`expr $i + $j`
i=$j
j=$k
echo $k
cnt=`expr $cnt + 1`
done

$ ./fibonacci.ksh
Enter n for Fibonacci series: -
10
Fibonacci series is: -
0
1
1
2
3
5
8
13
21
34
55

command for current day

$ echo `date +"%a"`
Sun

script to print file type in a directory

for filename in `ls -l`
do          
if [ -d $filename ]
    then
           echo $filename   it is a directory
                    elif [  -h $filename ]
                          then
                     echo $filename  it is a link
                     elif [ -f  $filename ]
                           then
                      echo $filename it is a file
fi
done

$ ./filetypecheck.ksh
filetypecheck.ksh it is a file
flashpoint it is a directory
new it is a file
new1 it is a file
newres it is a file
newrestu it is a file
newworld it is a file
test it is a directory

$ ls -l
total 12
-rwxr-xr-x 1 flash  363 Oct 29 23:31 filetypecheck.ksh*
drwxrwxr-x 2 flash4096 Oct 29 23:33 flashpoint/
-rw-rw-r-- 1 flash   0 Oct 29 23:22 new
-rw-rw-r-- 1 flash   0 Oct 29 23:22 new1
-rw-rw-r-- 1 flash   0 Oct 29 23:22 newres
-rw-rw-r-- 1 flash   0 Oct 29 23:22 newrestu
-rw-rw-r-- 1 flash   0 Oct 29  2016 newworld

drwxrwxr-x 2 flash 4096 Oct 29 23:22 test/

Monday 24 October 2016

Advanced redirection features

There are three types of I/O, which each have their own identifier, called a file descriptor:

standard input: 0
standard output: 1
standard error: 2


In the following descriptions, if the file descriptor number is omitted, and the first character of the redirection operator is <, the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is >, the redirection refers to the standard output (file descriptor 1).

ls>dirlist2>&1 will direct both standard output and standard error to the file dirlist, while the command

ls 2>&1 >dirlist will only direct standard output to dirlist.

It is an indication that the number that follows is not a file name, but rather a location that the data stream is pointed to. The greater-than sign should not be separated by spaces from the number of the file descriptor. If it would be separated, we would be pointing the output to a file again.

$ ls 2>temp

$ ls -l temp

-rw-rw-r-- 1 flash 0 Sept 7 12:58 temp

$ ls 2 >temp

ls: 2: No such file or directory


You can use the tee command to copy input to standard output and one or more output files in one move.

Using the –a option to tee results in appending input to the file(s). This command is useful if you want to both see and save output. The >and >>operators do not allow to perform both actions simultaneously.

This tool is usually called on through a pipe (|), as demonstrated in the example below:

$uptime | tee –a file1 file2

01:57:30 up 145 days, 6:57, 3 users, load average: 0.29, 0.29, 0.34

$ cat file2

Thu Jun 10 11:10:34 IST 2017

01:57:30 up 145 days, 6:57, 3 users, load average: 0.29, 0.29, 0.34

script to print zero to ten number

#!/bin/bash
num=0
while [ "$num" -lt 10 ];
do
echo "$num"
num=$((num+1))
done

In the above script we have first assigned zero value to variable num.
Then we are using while loop to iterate until the condition specified is valid,
That is it will continue to the operation till the num value is less than Ten.
do will perform the task specified.
echo will print the current value of num.


Output
0
1
2
3
4
5
6
7
8
9

script to find prime number

#!/bin/bash
echo -n "Enter a number: "
readnum
expr $num + 1  &> /dev/null

if [ $? -ne 0 ]
then
echo "You did not supplied number value"
exit 1
fi
[ $num -lt 2 ] && echo "Values < 2 are not prime numbers" && exit 1

i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo "$num is not a prime number"
echo "Since it is divisible by $i"
exit
fi
i=`expr $i + 1`
done
echo "$num is a prime number "

$ ./primecheck
Enter a number: 17
17 is a prime number

Commands and Meaning


Crontab

·        The cron system is managed by the crondaemon. It gets information about which programs and when they should run from the system's and users' crontab entries. Only the root user has access to the system crontabs, while each user should only have access to his own crontabs.
·        At system startup the cron daemon searches /var/spool/cron/ for crontab entries which are named after accounts in /etc/passwd, it searches /etc/cron.d/ and it searches /etc/crontab, then uses this information every minute to check if there is something to be done. It executes commands as the user who owns the crontab file and mails any output of commands to the owner.
·        You could also use the crontab-l command to display crontabs.
·        Some variables are set, and after that there's the actual scheduling, one line per job, starting with 5 time and date fields. The first field contains the minutes (from 0 to 59), the second defines the hour of execution (0-23), the third is day of the month (1-31), then the number of the month (1-12), the last is day of the week (0-7, both 0 and 7 are Sunday). An asterisk in these fields represents the total acceptable range for the field.
·        Lists are allowed; to execute a job from Monday to Friday enter 1-5 in the last field, to execute a job on Monday, Wednesday and Friday enter 1,3,5.
·        Keep in mind that output of commands, if any, is mailed to the owner of the crontab file. If no mail service is configured, you might find the output of your commands in your local mailbox, /var/spool/mail/<your_username>, a plain text file.
·        $ crontab -e
nocrontab for flash - using an empty one
##This script is to delete files and will run every 55th minute ##
55 * * * * /home/ flash /scripting/delete

Sunday 23 October 2016

Script to delete files from particular path

$ vi delete
#!/bin/ksh
LOGFILE=/home/ flash /logs/delete.log
touch $LOGFILE
echo " ------Starting Time `date` --------------- ">>$LOGFILE
cd /home/flash/enemy/bad
echo " ------FILE COUNT-----">>$LOGFILE
ls -lR /home/flash/enemy/bad/* | wc -l >>$LOGFILE
ls -ltr *.bad | awk '{print $9}' | xargs \rm

·        Here delete is the script name.
·        Firstly we have created a LOGFILE variable to create logs and defined the path where the log is to be created.
·        Touch command will create and empty file.
·        Echo command output will be redirected (>>) to LOGFILE in which current date stamp is added.
·        When you want to print output of any command inside echo command use : `  at the beginning and end of command.
E.g.: `date`
·        cd will change directory and then write the current count of files into LOGFILE by wc-l command
ls -lR /home/flash/enemy/bad/* | wc -l >>$LOGFILE
·        Here * means alll the types of file. We can specify type of extension to delete specific file.
E.g.: *.bad, *.txt
·        Last command is to remove files from the directory.
·        Then chmod 755 to change permissions of the file.
·        Now  run the script by ./scriptname and will remove the files.

Creating multiple files of particular extension in one line command.

·        To create and empty file we use command touch and filename.
Eg: touch filename1
Will create an blank filename1
·        In order to create multiple files use below command
touch filename{range}.extension
Eg: touch spl{001..100}.bad
·        Here .bad is extension and bspl is name and 001-100 is range.
·        It will create spl001.bad, spl002.bad…spl100.bad
·        $ pwd
/home/ flash /enemy/bad
·        $ ll
total 0
·        $ touch spl{001..100}.bad
·        $ ll| wc -l
101