Latest Blogs

Tuesday, February 8, 2022

Linux Shell Script Sample Code

Rem --------------------------------
Rem Sample shell script Linux
Rem --------------------------------
Rem 5.4.17-2136.300.7.el7uek.x86_64
Rem Oracle Linux Server 7.9
Rem This sample code is provided for educational purposes only, and is not supported by myself.
Rem It has been tested internally, however, we do not guarantee that it will work for you.
Rem Ensure that you run it in your test environment before using.
Rem For any feedback contact freely hayathk@hotmail.com
Rem 08-Feb-2022


---------------------------------------------------
Step#1 In linux or any editor write below sample code
---------------------------------------------------
vi test.sh

---------------------------------------------------
Step#2 Your script code
---------------------------------------------------
#! /bin/bash

echo "Mv statement started"
now=$(date +"%T")
echo "Current time : $now"

# you can also variable for path
sourcepath1=/home/oracle/test1
targetpath1=/home/oracle/test2
####replace your files and directory as per your requirement

#move all files
mv $sourcepath1/* $targetpath1

#move select files, uncoment below by removing #
#mv /home/oracle/test1/abc.txt /home/oracle/test2
#mv /home/oracle/test1/ggrun.txt /home/oracle/test2


echo "MV statement completed"
now=$(date +"%T")
echo "Current time : $now"


---------------------------------------------------
Step#3 Change execute permission for script
---------------------------------------------------
chmod +x test.sh

---------------------------------------------------
Step#4 Run or put in cron job
---------------------------------------------------
./test.sh >> mvfile.log 2>&1 &
or
38 12 * * * /home/oracle/test.sh >> /home/oracle/mvfile.log 2>&1


===================== scp example non-resumeable======================

#copy file to remote server through scp

# scp in background

# nohup scp file_to_copy user@server:/targetserverpath/file > scptransfer.out 2>&1

# copy all files mentioned in incrbackups.txt

# scp `cat myfileslist.txt` oracle@destserverIP:/targetfolder


===================== sftp example - resumeable ======================


--between oracle user, to avoid asking password for sftp session

ssh-keygen

ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host


bsftpScript.sh code::


#!/bin/bash

HOST='192.168.50.99'

USER='oracle'

###PASSWD='test123'

#currentDate=`date`

currentDate=`date +"%d-%m-%Y %T"`


echo "###################################"

echo "script started at $currentDate"


sftp $USER@$HOST  <<EOF

cd /home/oracle/test2

lcd /tmp

reget /home/oracle/test2/*.txt 

bye

EOF

echo "script completed at $currentDate"

echo "#################################"


cront entry:

12 10 * * * /home/oracle/bsftpScript.sh >> /home/oracle/bsftpScript.log 2>&1


Credit: 

https://www.linuxcommands.site/tech-topic/internet/how-to-run-a-script-in-the-background-in-linux/

https://www.servermania.com/kb/articles/how-to-use-sftp-commands/