Automatic ftp backup using cron job
#!/bin/sh
USERNAME=”your-ftp-user-name”
PASSWORD=”your-ftp-password”
SERVER=”your-ftp.server.com”
# local directory to pickup *.tar.gz file
FILE=”/tmp/backup”
# remote server directory to upload backup
BACKUPDIR=”/pro/backup/sql”
# login to remote server
ftp -n $SERVER <
cd $BACKUPDIR
mput $FILE/*.tar.gz
quit
EOF
Make sure script has executable permissions:
$ chmod +x /path/to/ftp.script.sh
Setup a cron job to run script at 15:30 (24 hr clock time) times:
30 15 * * * bash /path/to/ftp.script.sh
Categories: Shell Scripting Tags: cron job, shell script
How write a simple shell script and execute it
Following steps are required to write shell script:
(1) Use any editor like vi or mcedit to write shell script.
(2) After writing shell script set execute permission for your script as follows
syntax:
chmod permission your-script-name
Examples:
$ chmod +x your-script-name
$ chmod 755 your-script-name
Note: This will set read write execute(7) permission for owner, for group and other permission is read and execute only(5).
(3) Execute your script as
syntax:
bash your-script-name
sh your-script-name
./your-script-name
Examples:
$ bash bar
$ sh bar
$ ./bar
NOTE In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell, The syntax for . (dot) command is as follows
Syntax:
. command-name
Example:
$ . foo
Now you are ready to write first shell script that will print “Knowledge is Power” on screen. See the common vi command list , if you are new to vi.
$ vi first
#
# My first shell script
#
clear
echo “Knowledge is Power”
After saving the above script, you can run the script as follows:
$ ./first
This will not run script since we have not set execute permission for our script first; to do this type command
$ chmod 755 first
$ ./first
Categories: Shell Scripting Tags: linux shell script examples, shell script
Linux shell script to do arithmetic operations
Linux Shell script to do arithmetic calculations
#!/bin/bash
clear
echo “My calculator”
echo “1. Muliplication”
echo “2.Subtraction”
echo “3.Remainder”
echo “4 Divide”
echo -n “Please select your choice : ”
read choice
echo -n “enter your first number : ”
read n1
echo -n “enter your second number : ”
read n2
if [ $choice -eq 1 ]
then
answer=”$n1 x $n2 =$(($n1*$n2))”
elif [ $choice -eq 2 ]
then
answer=”$n1 – $n2 = $(($n1 – $n2))”
elif [ $choice -eq 3 ]
then
answer=”$n1 % $n2 = $(($n1 % $n2))”
elif [ $choice -eq 4 ]
then
answer=”$n1 / $n2 = $(($n1 / $n2))”
else
echo “Sorry slect number between 1 to 4″
exit 1
fi
echo $answer
Categories: Shell Scripting Tags: linux shell scripting
Simple shell script to calculate discount
Linux shell script to calculate discount
#!/bin/bash
echo -n “Enter price of an article : ”
read price
if [ $price -lt 100 ]
then
echo “No discount available”
d=0
else
echo “10% discount”
d=$(( $price * 10 / 100))
fi
pay=$(( $price – d ))
echo “You need to pay Rs : $pay “
Categories: Shell Scripting Tags: shell script
Shell script program to display calender and logined in users
This shell script will display calender and logined in users in Linux
echo “Today is :” $(date)
echo “Calender”
cal
echo “number of users currently loged in :” $(who|wc -l)
Categories: Shell Scripting Tags: shell script programs
