Script While Loop

Hello,
I'm working on a shell script that needs to read the a file (file1) a batch (read 2000 lines at a time) and then write those lines to seperate file which I'm using to run ldapmodify command. I need to check some other file size and once this file less than 200kb, i would need to run second batch and so on until file1 is empty.
Quote:
#!/bin/bash
set -x
filesize=200
server=10.11.xxx.xx
filename=fileimport.txt
taofile1=687686.txt
taofile=/var/opt/$taofile1

stty -echo
echo -n "Enter password: "
read passwd
stty echo
context="cn=user1,ou=org1,ou=org2,o=org"
if [[ -s "$filename" ]];then
for lines in $filename
do
head -99997 fileimport.txt >> ldapreadd.ldif && sed -i '1,+99996d' fileimport.txt
if [ -s ldapreadd.ldif ];then
ldapmodify -h $server -p 389 -D $context -w $passwd -v -x -f ldapreadd.ldif &> /dev/null
echo "LDIF script is running. Please wait..."
sleep 60
if [ -f "$taofile" ];then
taofilesize=`stat -c %s ${taofile}`
# if [ "$taofilesize" -ge 200 ];then
while [ `stat -c %s ${taofile}` -ge 200 ]
do
echo " Driver is processing LDIF file. Please wait..."
sleep 60
done
fi
rm -rf ldapreadd.ldif
fi
done
else
echo "$filename file is empty. Exiting script..."
exit
fi

I have 2 issues here. first one script is not reading exact lines as specified. Some times its reading more lines some times less lines. Second issues once it run the first batch, script is exiting. Please advise.


Similar Content



Best Way To Run Two Interdependent Scripts

Hi All,

I have two scripts, the aim of these two scripts is, to check whether a particular script is running or not, if it wont runs, then throw a mail.

How i Achieved this output is, I wrote first script.

I created an infinite while loop which performs below steps

1. It creates a touch file
2. Triggers the script which needs to be monitored if its working or not.
3. Removes the touch file.

If the second step fails, then the remove file command will not happen and the script will stuck there itself.

I created an another script which checks the creation time of the touch file and if it is more than ten minutes, it means the second step in the first script is hanged, which also means that particular script is not working.

So if the creation time is more than 2 minutes the second script will throw a mail.

Below are the two scripts.

Code:
#!/bin/ksh



userid="chansd"

filename="/apps/log/check.txt"

while true ;do
touch $filename
pass=`/apps/eDMZ/call_st.ksh $userid`
sleep 20
rm $filename
done

Below script checks the file creation time and throws email if it is older than 2 minutes
Code:
#!/bin/ksh


filename="/apps/log/check.txt"

if [ -f "${filename}" ]
then
if test "`find $filename -mmin +2`"
then
echo "script is not working ! Please act on it" | mail -s "Script  is not working" Example@mail.com
fi


else

        exit 1

fi


What im going to do is

1. I am going to run the first script in background so it runs forever.
2. I am going to run the second script in cron forevry 5 mins to check the file creation time.

3. So if the first script hangs . I will kill the process using process id and after the issue resolves with the inner script, I will run the main script again.

I am new to Linux, Please let me know if this approach will work as expected.

String Concatenation In UNIX / Shell Script

Hi,

I am new to UNIX - i wrote this below script based on the requirement. But i am stuck at the concatenation (at the second last step of the code)

The below code is working fine till the concatenation(second last step) - I need to concatenate Hello to the "physId" e.g. - The filename is UM123456789.20150503 - i am extracting M123456789 and i need to append "HELO" to it at the end. But as per the below script - when i am using the concatenation, it is overwriting the M123456789 and the output thus becomes HELO456789. I am trying to get the output as - M123456789HELO - where am i going wrong?

on the terminal i checked - echo $0 and it gave -> /bin/sh. Hence i wrote the below code.

#!bin/sh
absolutePath=/abc/data/abc_unix/stg/decrypt/*.*
filepath=$(echo ${absolutePath%.*})
echo "$filepath"
filenameext=$(echo ${filepath#/abc*decrypt/})
echo "$filenameext"
file=$(echo ${filenameext#.*})
echo "$file"
extract_physId=$(echo ${file:1:9})
physId=$(echo ${extract_physId})
echo "$physId"
key="$physId"HELO
echo "$key"

Script To Find The File Creation Time Is More Than Ten Minutes

Hi All,

Im trying to create a script which checks the creation time of a file and if it is more than ten minutes, send out an alert.

For ex : Creation date of file is 10:30 AM
current time is 10:45 AM

Then send an alert/ message.

This is the script i wrote below :

Code:
#!/bin/ksh

filename="/apps/log/file.txt"

if [ -f "${filename}" ]
then
        createTime=`ls -lad "${filename}" | awk '{print $8}'`
        echo "$createTime"
        currentTime=`date '+%M'`
        echo "$currentTime"
        DIFF=$(( $currentTime - $createTime )) 
        echo "$DIFF"

else

        exit 1

fi

I am getting syntax error on the subraction when i try to run this script . I can understand that creation time and current time is in different format thats y this error throws, but i dont know how to rectify it .

I need to find out if the file creation time is more than ten minutes.

Please help me in achieving this output.

Bash Scrip Running On Remote Server

This is my script and the syntax to run this script is give ip and next will be the file or script you want to perform on remote server



#!/bin/bash

# The private key used to identify this machine
IDENTITY_KEY=/home/adnew.pem

syntax()
{
echo "Syntax: Ec2.sh server_ip scriptFile]"
echo "For example: ./Ec2.sh server_ip scriptFile"
exit 1
}

if [ $# -ne 2 ]
then
echo not enough arguments
syntax
fi


echo "Running script $2 on $1"
ssh -ttq -i $IDENTITY_KEY ec2-user@$1 sudo -i 'bash -s' < $2
exit
exit
echo "Done"



on script file i have give for testing

touch /root/test
ls /root/test
exit
exit

it makes the file but do not show the ls output by giving error


tcgetattr: Inappropriate ioctl for device

exit


what I have to do ??

Better Way Of Writing This Command Of Leaving Multiple Blank Lines

Hi All..

I need to give four lines of gap(Freespace) between two command outputs.

what i did was below

Code:
echo "" >>$LOG_DIR/$LOG_NAME
echo "" >>$LOG_DIR/$LOG_NAME
echo "" >>$LOG_DIR/$LOG_NAME
echo "" >>$LOG_DIR/$LOG_NAME
print "***************** LEVEL 2: Truncating  Tables: Success:  tables Truncated ***********************" >> $LOG/$LOG_NAME
echo "" >>$LOG_DIR/$LOG_NAME
echo "" >>$LOG_DIR/$LOG_NAME
echo "" >>$LOG_DIR/$LOG_NAME
echo "" >>$LOG_DIR/$LOG_NAME
# Run the load scripts.
echo "***************** LEVEL 3: Loading Dupe Check Tables: Success: Dupe Check tables Loaded***********************" >> $LOG/$LOG_NAME


Is there any better way of writing this command to leave multiple lines between two commands.

Please advise...

Inquiry On A Bash Script Using Sed And Grep -c

Hi Everyone,

I need some help on my bash script, I'm trying to rename a certain line in a file which might be 1 or more.

IDXCOUNT=`grep -c 'index .* on ' $FILENAME`;

for n in $(eval echo {1..$IDXCOUNT});
do
timestamp=$(date +"%s");
echo "Renaming index idx_$timestamp..";
if [ $n -eq 1 ]; then
sed -i "0,/^index [^)]* on /s/index idx_$timestamp on /" $FILENAME;

My problem is, if the sed target is 2 or more it will generate an idx_$timestamp that will cause duplicate index value when the script finish. My goal is to have the script recognize that when there are multiple index in the file, it will try to rename it one by one.

I'm new to bash so I'm not sure if I explained my issue well but I will appreciate any help!!

Thanks!

Tips On Using Bash Script To Edit A Windows Text File

I have a bash script executing on an AIX server to a windows share directory to update a windows text file. Most of the file is edited as expected but I have 3 issues that I am not sure how to resolve.

1. Windows text file contains a double backslash (\\) and the \\ is being written back as a single \ which corrupts the file.
2. I am not getting the last line of text in the file written back out.
3. Some lines of text are not written back out completely.

Here is a snippet of my bash script:
cp $Directory/Somefile $Directory/Somefile.$(date +'%Y%m%d')
cat /dev/null > ${HOME}/temp.out

while read -r inpLine; do
patternFound=n
echo ${inpLine} | grep -q 'SOMEPATTERN='
if [ $? = 0 ]; then
patternFound=y
echo "##${inpLine}" >> ${HOME}/temp.out
echo "SOMEPATTERN=newValue" >> ${HOME}/temp.out
fi

if [ ${patternFound} = "n" ]; then
echo ${inpLine} >> ${HOME}/temp.out
fi

done < $Directory/Somefile
mv ${HOME}/temp.out $Directory/Somefile

Rsync Script Problem

Hi all,

I am trying to write a script that syncs files from source to destination. I have one centralized server that can ssh to any servers without pw. Now when I run the script, it can ssh to source server perfectly fine, but you need to enter password for destination server. Was wondering how I can clean this up before I start using case statements

Below is a sample I wrote

#!/bin/bash

#This scripts syncs shit

echo "Type in ID: "
read ID

echo "Type in Server : " #source server
read S

echo "Type in Destination Server: "
read DS

if [ $S == 9 ]; then
ssh -t "root@"$S"webserver1" "rsync -av /home/rlui/"$ID "root@"$DS"webserver2:/home/rlui/";
ssh -t "root@"$S"webserver1" "rsync -av /home/rlui/tmp/"$ID "root@sl"$DS"webserver2:/home/rlui/tmp/"
exit 1

where S and DS are cluster numbers

I apologize in advance if I am not clear on anything

Check Empty Variable Without Comments

Hello,

They gave me this:
Code:
#!/bin/bash

# Write error message on stderr and die
function die() {
  echo "$@" >&2
  exit 1
}

# Load var from properties files
function load() {
  for propertie in "$@" ; do
    [[ -f "${propertie}" ]] || die "load() : \"${propertie}\" don't exist !"
    while read ; do
echo "${REPLY%=*}"
echo "${REPLY#*=}"
      [[ -n "${REPLY%%=*}" && -z "${REPLY#*=}" ]] || die "load() : Variable \"${REPLY%%=*}\" is empty!"
      eval "${REPLY%%=*}"=\'"${REPLY#*=}"\'
    done < "${propertie}"
  done
}

load "manage_srcds.conf"
echo "SRCDS_TICKRATE=$SRCDS_TICKRATE"
echo "SRCDS_SCREEN=$SRCDS_SCREEN"
echo "SRCDS_SRCDS_GAME_NAME=$SRCDS_GAME_NAME"
echo "SRCDS_MAXPLAYERS=$SRCDS_MAXPLAYERS"

but, return:
Quote:
###################################################
###################################################
load() : Variable "###################################################" is empty!

cat manage_srcds.conf

I just wish that there was no empty variable excluding those who do not have integer.

Best regards,

Help With 'CASE'

Hi guys,

i'm beginner with Unix, I tried my best, but I Really don't know how to finish it .

here's my problem : I have to do something like Student system - student's name | subjects | mark |credits.

after starting script it will ask you for Student's name, then which subject, then the mark will appear.

It's a homework, and I have my own idea how to solve it, but cannot finish it, i don't want complete solution, just a help!

Code:
#!/bin/bash
echo "Students name"
echo -n "Enter student's name: "
read name
echo
case $name in
Example1|Math) echo "C" ;;
Example2|Programming) echo "D" ;;
Example3|WWW) echo "A" ;;
Example4|Economie) echo "-" ;;
esac

I don't now how to bring together case with second things which is "enter subject".

I appreciate any help