I am having problem in understanding the following line of code ..
/home/rmsbatch/autoscript/autorms.ksh dc_load_main.ksh -q belk_dc_load_tran_data.seq
What's being done here ? what does "-q" means ? What does ".seq" file means in unix "belk_dc_load_tran_data.seq"
Please elaborate
autorms.ksh
#!/bin/ksh
################################################################################
# Description : Execute RMS Jobs with Error Reporting
#
################################################################################
. /home/rmsbatch/.profile
set -x
LOG=/logs/IBM/AutoLogs
CMNLOG=$LOG/BatchStatus_`date +"%y%m%d`.txt
if [ "${1}" == "prepost" ] || [ "${1}" == "bprepost" ]
then
exec > ${LOG}/Auto_${1}_${3}.log 2>&1
else
exec > ${LOG}/Auto_${1}.log 2>&1
fi
function Error_Log
{
# translate "\n" to "^" below
v1=$(echo -n $1 | tr "\n" "^")
v2=$(echo -n $2 | tr "\n" "^")
echo $(hostname)"|"$(basename $CMD $PARAM3)"|"$(date +%m/%d/%y)"|"$(date +%H:%M:%S)"|"$v1"|"$v2 |grep "Failed"
if [[ $? -eq 0 ]]
then
echo $(hostname)"|"$(basename $CMD $PARAM3)"|"$(date +%m/%d/%y)"|"$(date +%H:%M:%S)"|"$v1"|"$v2 | mail -s "RMS Batch Failed in PROD" rms_app_support#belk.com,bandrest#in.ibm.com,vanarsda#us.ibm.com,wgwinslo#us.ibm.com
fi
echo $(hostname)"|"$(basename $CMD $PARAM3)"|"$(date +%m/%d/%y)"|"$(date +%H:%M:%S)"|"$v1"|"$v2 >> $CMNLOG
}
function RunBatch
{
set -x
echo "Running the Batch or Script"
echo "Command" $CMD
if [[ $check -ne 1 ]]
then
ls $MMBIN/$CMD
if [ $? == 0 ] || [ $uRC -eq 0 ]
then
echo "------ Running the Command ------"
Error_Log "$(basename $CMD $PARAM3) Started"
if [ "${CMD}" == "prepost" ] || [ "${CMD}" == "bprepost" ]
then
${MMBIN}/${CMD} $PARAM1 $PARAM3 $PARAM4 $PARAM5
echo $?| read VResult
else
cd $MMBIN
$SHOME/batch_wrapper.ksh ${CMD} $PARAM1 $PARAM3 $PARAM4 $PARAM5
echo $?| read VResult
fi
if [ $VResult -eq 0 ]
then
Error_Log "$(basename $CMD $PARAM3) Batch Completed Successfully"
VResult=0
return $VResult
else
cat $MMHOME/error/err.${CMD}*.`date +"%b_%d"`|tail -1|grep error
echo $error|read Error
echo `$SHOME/batch_wrapper.ksh ${CMD} $PARAM1 $PARAM3 $PARAM4 $PARAM5`|read Error1
Error_Log "$(basename $CMD $PARAM3) Failed with - $Error Error1"
return $VResult
fi
else
echo " Command not found in the Directory "
Error_Log "$(basename $CMD $PARAM3) Failed for Command Not Found"
VResult=99
return $VResult
fi
else
echo "------ Running the Command ------"
Error_Log "$(basename $CMD $PARAM3) Started"
$MMHOME/external/scripts/$CMD.ksh $PARAM1 $PARAM3 $PARAM4 $PARAM5 $PARAM6 $PARAM7
echo $?|read Result
VResult=$Result
if [ $VResult -eq 0 ]
then
Error_Log "$(basename $CMD $PARAM3) Batch Completed Successfully"
return $VResult
else
Error_Log "$(basename $CMD $PARAM3) Failed"
return $VResult
fi
fi
}
if [[ $# -gt 0 ]]
then
echo $1|grep ksh
if [[ $? -ne 0 ]]
then
CMD=$1
else
echo $1|awk -F"." '{print $1}'|read CMD
check=1
fi
LOGIN=$UP
PARAM1=$2
PARAM3=$3
PARAM4=$4
PARAM5=$5
PARAM6=$6
PARAM7=$7
MMBIN=$MMHOME/oracle/proc/bin
SHOME=/home/rmsbatch/autoscript
echo " Shell Started the Command :" ${CMD}
echo " Parameters Passed are :" $*
echo " MM Home Directory :" $MMHOME
RunBatch
else
echo "##############################################################################################################################"
echo "# Not Enough Parameter Passed Or Usage of Parameter is not Proper"
echo "# value to be Passed to the Current Shell is : " $#
echo "# Command being executed is : " $CMD
echo "# Usage of current command is given below "
echo ""
echo "" `$MMHOME/oracle/proc/bin/$1`
echo "##############################################################################################################################"
return 99
fi
autorms.ksh is probably a shell script that takes 3 parameters...
the first of which looks to be another script (dc_load_main.ksh) but it is only a guess and not necessarily the case
the second is -q which could mean anything
the third is probably another file but you cannot tell what the file should contain as extensions are fairly arbitrary and not enforced in Unix.
In short, you need to read autorms.ksh to know anything at all.
Related
I want to check for file in directory if there then push it to ssh server checing server connection if file not there then try 3 times with each 1min interval and in between if it comes ( on 2nd attend for example) then try again to connect ssh and push. else check for 3 attempts and exit
Please check my below code it is halting after 1st attempt ( during 2nd attempt I am making file available)
#!/bin/sh
echo "OK, start pushing the Userdetails to COUPA now..."
cd /usr/App/ss/outbound/usrdtl/
n=0
until [ $n -ge 3 ] || [ ! -f /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv ]
do
if [ -f /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv ] ;
then
pushFiles()
else
n=$[$n+1]
sleep 60
echo " trying " $n "times "
fi
done
pushFiles()
{
echo "File present Now try SSH connection"
while [ $? -eq 0 ];
do
echo $(date);
scpg3 -v /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv <sshHost>:/Incoming/Users/
if [ $? -eq 0 ]; then
echo "Successfull"
echo $(date);
echo "Successfull" >> /usr/App/ss/UserApproverDetails.log
exit 1;
else
echo $(date);
echo "Failed" >> /usr/App/ss/UserApproverDetails.log
echo "trying again to push file.."
scpg3 -v /usr/App/sg/outbound/usrdtl/USERS_APPROVERS_*.csv <ssh Host>:/Incoming/Users/
echo $(date);
exit 1;
fi
done
}
I've tried to simplify this code for you. I hope it helps:
#!/bin/bash
outdir="/usr/App/ss/outbound/usrdtl"
logfile="/usr/App/ss/UserApproverDetails.log"
file_prefix="USERS_APPROVERS_"
function push_files() {
echo "File present now try SSH connection"
local attempts=1
local retries=2
date
while [[ ${attempts} -lt ${retries} ]]; do
if scp ${outdir}/${file_prefix}*.csv <sshHost>:/Incoming/Users/ ; then
echo "Successful" | tee -a ${logfile}
date
exit 0
else
echo "Failed" >> ${logfile}
fi
attempts=$((attempts+1))
do
echo "scp failed twice" | tee -a ${logfile}
exit 2
}
echo "OK, start pushing the Userdetails to COUPA now..."
cd ${outdir}
attempts=1
retries=3
while [[ ${attempts} -lt ${retries} ]]; do
echo "looking for files...attempt ${attempts}"
if test -n "$(shopt -s nullglob; echo ${outdir}/${file_prefix}*.csv)"; then
push_files()
fi
attempts=$((attempts+1))
sleep 60
done
echo "Files were never found" | tee -a ${logfile}
exit 1
Look at this code and tell me how it's not doing what you're trying to do. The most complicated part here is the nullglob stuff, which is a handy trick to see if any file in a glob matches
Also, I generally used bashisms.
#!/bin/bash
helpFunc()
{
echo "Usage:
$0 --arg1
$0 --arg2 filename add/delete/replace "hi how are you"
$0 --help
"
}
function fileread() {
pwd
echo "Fileread Function"
if [ -f $filename ]
then
echo "file exists"
filename=$1
cat $1
case "$1" in
add|ADD)
read $1
if [$2 == 'add' ]
then
echo $3 >> $1
echo "add opeartion completed"
fi
shift
;;
delete|DELETE)
read $1
echo "echo delet string from file"
if [$2 == 'delete' ]
then
sed '/$3/d' $1
echo "add opeartion completed"
fi
shift
;;
replace|REPLACE)
read $1
if [ $4 == 'replace' ]
then
sed "s/$5/$6/g" "$1" > temp.txt
else
echo "Error: Cannot read $1"
fi
shift
;;
esac
shift
# return 0
else
echo "file not exist"
#return 1
fi
}
while [ $# ]
do
case "$1" in
--a)
if [ -z "$2" ]
then
echo "No argument supplied"
echo "hello" && echo " current process id : $$ "
else
echo -e "no need of sending arguments for --a"
helpFunc a;
exit 1
shift
fi
;;
--b)
if [ $( echo -e $2 | grep -c -E "^-" ) -eq 0 ]
then
fileName=$2
if [ -f "$fileName" ]
then
echo -e "The file is valid"
echo "Displaying $2 file..."
[ -z $fileName ] && { echo "File name missing"; exit 1; } || cat $fileName
case "$filename" in
add|ADD)
echo "adding"
fileread $3 $4
shift
;;
delete|DELETE)
echo "echo deleting"
fileread
shift
;;
replace|REPLACE)
echo "replacing"
fileread
shift
;;
esac
else
echo -e "please enter the valid file name or location"
helpFunc b;
exit 1
shift
fi
fi
esac
shift
done
exit 0
function file read should ignore the case inputs like ADD/add , delete/Delete etc.. and perform operations if i give the cmd line arguments and values and it should validate case sensitivity, file existence and path of the file
./script.sh -a -b /etc/opt/test.txt ADD "hi how are you" delete "i am fine" REPLACE "hi " "hey"
I'm trying to write recursive scripts in bash that receive as an argument a single path and prints the depth of the directory tree rooted at this path.
This is the list_dirs.sh script:
ls -l $dir | grep dr..r..r.. | sed 's/.*:...\(.*\)/\1/'
And this is the isdir.sh script:
if [ -d $1 ]; then
echo 1
elif [ -e $1 ]; then
echo 0
else
echo -1
fi
They both work good.
This is the script dir_depth.sh that I wrote that doesn't work:
if [ $# -eq 0 ]; then
echo "Usage: ./dir_depth.sh <path>"
exit1
fi
x=`source isdir.sh $1`
if [ $x -eq -1 ]; then
echo "no such path $1"
fi
dir=$1
maxD=0
dirs=`source list_dirs.sh`
for f in $dirs
do
if [ $x -ne 0 ]; then
x=`dir_depth.sh $f`
if [ "$x" -eq "$maxD" ]; then
maxD=x;
fi
fi
echo $f
done
echo $((maxD++))
I'm really new to bash scripting and I don't know how to debug or what's wrong in my script.
Some missing items are:
If you have a directory parent/child/ and run list_dirs.sh parent/, it will output child. You then try to look up child/ in the current directory instead of parent/child/.
You do echo $f for debug purposes and echo $((maxD++)) to return a result. They are being confused for each other. Use >&2 to write errors and debug messages to stderr.
echo $((maxD++)) is a classic error equivalent to return x++. You return the number, and then increment a variable that's no longer used.
[ "$x" -eq "$maxD" ] makes no sense. Use -ge since you're trying to find the max.
Here's dir_depth.sh with these changes in place:
if [ $# -eq 0 ]; then
echo "Usage: ./dir_depth.sh <path>" >&2
exit 1
fi
x=`source ./isdir.sh $1`
if [ $x -eq -1 ]; then
echo "no such path $1" >&2
fi
dir=$1
dirs=`source ./list_dirs.sh`
maxD=0
for f in $dirs
do
if [ $x -ne 0 ]; then
x=`./dir_depth.sh "$1/$f"`
if [ "$x" -ge "$maxD" ]; then
maxD="$x";
fi
fi
echo $f >&2
done
echo $((maxD+1))
have some problem with shell script.
In our office we set up only few commands, that available for devs when they are trying ssh to server. It is configured with help of .ssh/authorized_keys file and available command for user there is bash script:
#!/bin/sh
if [[ $1 == "--help" ]]; then
cat <<"EOF"
This script has the purpose to let people remote execute certain commands without logging into the system.
For this they NEED to have a homedir on this system and uploaded their RSA public key to .ssh/authorized_keys (via ssh-copy-id)
Then you can alter that file and add some commands in front of their key eg :
command="/usr/bin/dev.sh",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty
The user will do the following : ssh testuser#server tail testserver.example.com/2017/01/01/user.log
EOF
exit 0;
fi
# set global variable
set $SSH_ORIGINAL_COMMAND
# set the syslog path where the files can be found
PATH="/opt/syslog/logs"
# strip ; or any other unwanted signs out of the command, this prevents them from breaking out of the setup command
if [[ $1 != "" ]]; then
COMMAND=$1
COMMAND=${COMMAND//[;\`]/}
fi
if [[ $2 != "" ]]; then
ARGU1=$2
ARGU1=${ARGU1//[;\`]/}
fi
if [[ $3 != "" ]]; then
ARGU2=$3
ARGU2=${ARGU2//[;\`]/}
fi
if [[ $4 != "" ]]; then
ARGU3=$4
ARGU3=${ARGU3//[;\`]/}
fi
# checking for the commands
case "$COMMAND" in
less)
ARGU2=${ARGU1//\.\./}
FILE=$PATH/$ARGU1
if [ ! -f $FILE ]; then
echo "File doesn't exist"
exit 1;
fi
#echo " --------------------------------- LESS $FILE"
/usr/bin/less $FILE
;;
grep)
if [[ $ARGU2 == "" ]]; then
echo "Pls give a filename"
exit 1
fi
if [[ $ARGU1 == "" ]]; then
echo "Pls give a string to search for"
exit 1
fi
ARGU2=${ARGU2//\.\./}
FILE=$PATH/$ARGU2
/usr/bin/logger -t restricted-command -- "------- $USER Executing grep $ARGU1 \"$ARGU2\" $FILE"
if [ ! -f $FILE ]; then
echo "File doesn't exist"
/usr/bin/logger -t restricted-command -- "$USER Executing $#"
exit 1;
fi
/bin/grep $ARGU1 $FILE
;;
tail)
if [[ $ARGU1 == "" ]]; then
echo "Pls give a filename"
exit 1
fi
ARGU1=${ARGU1//\.\./}
FILE=$PATH/$ARGU1
if [ ! -f $FILE ]; then
echo "File doesn't exist"
/usr/bin/logger -t restricted-command -- "$USER Executing $# ($FILE)"
exit 1;
fi
/usr/bin/tail -f $FILE
;;
cat)
ARGU2=${ARGU1//\.\./}
FILE=$PATH/$ARGU1
if [ ! -f $FILE ]; then
echo "File doesn't exist"
exit 1;
fi
/bin/cat $FILE
;;
help)
/bin/cat <<"EOF"
# less LOGNAME (eg less testserver.example.com/YYYY/MM/DD/logfile.log)
# grep [ARGUMENT] LOGNAME
# tail LOGNAME (eg tail testserver.example.com/YYYY/MM/DD/logfile.log)
# cat LOGNAME (eg cat testserver.example.com/YYYY/MM/DD/logfile.log)
In total the command looks like this : ssh user#testserver.example.com COMMAND [ARGUMENT] LOGFILE
EOF
/usr/bin/logger -t restricted-command -- "$USER HELP requested $#"
exit 1
;;
*)
/usr/bin/logger -s -t restricted-command -- "$USER Invalid command $#"
exit 1
;;
esac
/usr/bin/logger -t restricted-command -- "$USER Executing $#"
The problem is next:
when i try to exec some command, it takes only first argument, if i do recursion in files by using {n,n1,n2} - it doesn't work:
[testuser#local ~]$ ssh testuser#syslog.server less srv1838.example.com/2017/02/10/local1.log |grep 'srv2010' | wc -l
0
[testuser#local ~]$ ssh testuser#syslog.server less srv2010.example.com/2017/02/10/local1.log |grep 'srv2010' | wc -l
11591
[testuser#local ~]$ ssh testuser#syslog.server less srv{1838,2010}.example.com/2017/02/10/local1.log |grep 'srv2010' | wc -l
0
[testuser#local ~]$ ssh testuser#syslog.server less srv{2010,1838}.example.com/2017/02/21/local1.log |grep 'srv2010' | wc -l
11591
Could someone help me, how can i parse\count command arguments to make it work?
Thank you and have a nice day!
The number of arguments for a bash script would be $#. As a quick example:
#!/bin/bash
narg=$#
typeset -i i
i=1
while [ $i -le $narg ] ; do
echo " $# $i: $1"
shift
i=$i+1
done
gives, for bash tst.sh a b {c,d}
4 1: a
3 2: b
2 3: c
1 4: d
In your script, the command to execute (cat, less, ...) gets explicitly only the second argument to the script. If you want to read all arguments, you should do something like this (note: only a hint, removed all sorts of checks etc..)
command="$1"
shift
case $command in
(grep) pattern="$1"
shift
while [ $# -gt 0 ] ; do
grep "$pattern" "$1"
shift
done
;;
esac
note: added some quotes as comment suggested, but, being only a hint, you should carefully look at quoting and your checks in your own script.
Less command working now:
case "$COMMAND" in
less)
if [[ $ARGU1 == "" ]]; then
echo "Pls give a filename"
exit 1
fi
FILES_LIST=${#:2}
FILE=(${FILES_LIST//\.\./})
for v in "${FILE[#]}";do
v=${v//[;\']/}
if [ ! -f $v ]; then
echo "File doesn't exist"
fi
/usr/bin/less $PATH/$v
done;;
tail command works too with 2 and more files, but i can't execute tail -f command on two files unfortunately.
Provided below is executable file. through script i need to endpoints to it in optimized manner.
filename : taoexec
#!/bin/bash
. /etc/init.d/functions
PIDFILE=/var/run/Naming_Service.pid
PORT=
OPTIONS="-p ${PIDFILE}"
RETVAL=0
prog="Naming_Service"
start() {
echo -n $"Starting $prog: "
if [ $UID -ne 0 ]; then
RETVAL=1
failure
else
setsid /usr/local/bin/Naming_Service ${OPTIONS} &
RETVAL=$?
fi
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
if [ $UID -ne 0 ]; then
RETVAL=1
failure
else
killproc /usr/local/bin/Naming_Service
RETVAL=$?
fi
echo
return $RETVAL
}
restart(){
stop
start
}
case "$1" in
start) start ;;
stop) stop ;;
restart) restart ;;
status) status -p ${PIDFILE} ${prog}; RETVAL=$? ;;
*) echo "Usage: $0 {start|stop|status|restart}"; RETVAL=1
esac
from Environment variables I'm reading the port numbers and I try to replace port and hostname into that file. While i'm using below mentioned script ,not achieved. let me know how i can achieve this
NS_HOSTNAME=$(grep "`hostname`" /etc/hosts|awk '{print $1}')
namingService_ports=$NS_PORTS
echo $namingService_ports
IFS=','
read -r -a portArray <<< "$namingService_ports"
for num in "${portArray[#]}";
do
sed 's~^\(OPTIONS\)="\(-p \${PIDFILE\)}~\1'"_$num"'="\2'"_$num"'} -ORBEndpoint iiop://'"$NS_HOSTNAME"':'"$num"'~' <<< 'OPTIONS="-p ${PIDFILE}"' >> "/etc/init.d/tao"
done
I'm expecting the Following output at last, if I provided NS_PORTS=13021,13022,13023
#!/bin/bash
. /etc/init.d/functions
PIDFILE_13021=/var/run/Naming_Service_13021.pid
PIDFILE_13022=/var/run/Naming_Service_13022.pid
PIDFILE_13023=/var/run/Naming_Service_13023.pid
PORT=
OPTIONS_13021="-p ${PIDFILE_13021} -ORBEndpoint iiop://10.12.23.34:13021"
OPTIONS_13022="-p ${PIDFILE_13022} -ORBEndpoint iiop://10.12.23.34:13022"
OPTIONS_13023="-p ${PIDFILE_13023} -ORBEndpoint iiop://10.12.23.34:13023"
start() {
echo -n $"Starting $prog: "
if [ $UID -ne 0 ]; then
RETVAL=1
failure
else
setsid /usr/local/bin/Naming_Service ${OPTIONS_13021} &
setsid /usr/local/bin/Naming_Service ${OPTIONS_13022} &
setsid /usr/local/bin/Naming_Service ${OPTIONS_13023} &
RETVAL=$?
fi
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
if [ $UID -ne 0 ]; then
RETVAL=1
failure
else
killproc /usr/local/bin/Naming_Service
RETVAL=$?
fi
echo
return $RETVAL
}
restart(){
stop
start
}
case "$1" in
start) start ;;
stop) stop ;;
restart) restart ;;
status) status -p ${PIDFILE} ${prog}; RETVAL=$? ;;
*) echo "Usage: $0 {start|stop|status|restart}"; RETVAL=1
esac
Anybody can give the solution for this.
Is there any reason you can't just generate the file from scratch each time the script runs?
#!/bin/bash
FILENAME=/etc/init.d/tao
NS_HOSTNAME=$(grep "`hostname`" /etc/hosts|awk '{print $1}')
namingService_ports=$NS_PORTS
echo $namingService_ports
cat > "$FILENAME" << EOF
#!/bin/bash
. /etc/init.d/functions
PORT=
EOF
IFS=','
read -r -a portArray <<< "$namingService_ports"
for num in "${portArray[#]}" ; do
echo PIDFILE_$num=/var/run/Naming_Service_$num.pid
echo OPTIONS_$num=\"-p \${PIDFILE_$num} -ORBEndpoint iiop://${NS_HOSTNAME}:$num\"
done >> "$FILENAME"