Solved

Korn Shell Script Problem!

Posted on 2001-06-24
12
572 Views
Last Modified: 2013-12-27
I have a problem I have to write a Korn Shell script that takes its input from a flat file and then runs as a batch job every 5am. My questions are how do I get the script to take input from a flat file I have never written a script that way? And second How do I schedual a job with cron to run every 5am?
Thanks!
0
Comment
Question by:liam_2e
12 Comments
 
LVL 40

Expert Comment

by:jlevie
Comment Utility
How you get the input from the file depends to a great degree on what you get from the file and how it is used. A more complete description on what needs to be done would be a big help.

You schedule your script by adding it to the crontab of the appropriate user (probably root) with "crontab -e". To execute a script at 5am the crontab entry would look like:

0 5 * * * /path-to/your-script

See the man pages for crontab for more information.
0
 
LVL 4

Expert Comment

by:newmang
Comment Utility
You can read from a flat file as follows:

#!/usr/bin/ksh
FILE=/home/myfilename
#
# If the address file does not exist there is no point in going on!
if [[ ! -f $FILE ]]
then
    exit 1
fi
#
# Open the file using descriptor 3
exec 3< $FILE
#
# Iterate through each line in the file
while read -u3 LINE
   do
      do what you want with the contents of $LINE
   done
#
# Close the descriptor & file
exec 3<&-
#
# Exit with good result
exit 0

Running each day at 0500 is as per the previous post in this thread.

Cheers - Gavin
0
 
LVL 1

Expert Comment

by:k.kidambi
Comment Utility
If you can post the input (format of the file) and what are all the parameters you are taking from the file and also what do you need to get the output. Then I can probably post the script per your requirement.

Kidambi
0
 

Author Comment

by:liam_2e
Comment Utility
One problem I am going to have is I am passing the input from the text file to a Vignette command and this command will require a variable to have space between ex

transferproject -b computer1:port2134:/pr/34r -o export -t "/ci/34r /ci/324r /ci/34r" -p exportname -l username:password

The problem I am showing is that the stuff in quotes must be read as one variable so I can drop it into the command but there has to be space between the template ids (the ci stuff) for the transferproject command to work. I don't know how to do this if the input variables start and end with a space???
0
 
LVL 4

Accepted Solution

by:
jlms earned 105 total points
Comment Utility
Confussing...

Post a piece of the file, explain what you need to get, and you'll surely get a nice script.
0
 
LVL 1

Expert Comment

by:k.kidambi
Comment Utility
All you need to do is change the delimiter in the text file where you are calling your vairables.

Here is a sample file that contains your variables.
filename is /yourhome/variables.txt (this file is delimited with ":")

computer1:port2134:/pr/34r" "export:/ci/34r /ci/324r /ci/34r:exportname:username:password

#/bin/ksh

VAR1=`cat variables.txt | awk -F":" '{print $1}'`

similarly you can declare other variables also and then run the follwoing command

transferproject -b ${VAR1}:${VAR2}:${VAR#} -o ${VAR4} -t "${VAR5}" -p ${VAR6} -l ${VAR7}:${VAR8}

Try this out and post your comments here.

Kidambi



0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:liam_2e
Comment Utility
Ok here is what the script looks like now, and it works basically it prompts the user for the info that it needs. What I want is to have the info that the user is usually prompted for to be in a text file delimited by ":" although I dont know how to tell the script to read the file and create those variables! I need to have the variable data delimited by ":" because some variables may have some space in them that must be recognize. Well here is my script it is used to transfer vignette projects and templates.

Thanks!!!!!!!!!!!!!!!!!!!



echo "Vignette Project/Template/Data Transfer Script"

sleep 1;

echo " "

echo "Are you going to transfer more than one Template/Record?"
echo "Answer n if you are tranfering a Project y/n: \c"
read transfernum
echo "What is the name of the Source Server that you want to transfer Vignette"
echo "files from?: \c"
read exportmach
echo "What is the name of the Target Server that you want to transfer"
echo "files into?: \c"
read importmach
echo "What is the username to login to the Source Vignette Server?: \c"
read usernm
echo "What is the password to login to the Source Vignette Server?: \c"
read passwrd
echo "What is the Management ID of the Project, or Parent directory"
echo "of the files you would like to transfer?: \c"
read prjnm
echo "What is the Management ID of the Parent directory where the export"
echo "should be imported into?: \c"
read pardir
echo "What would you like to name your exported file?: \c"
read bkpnm


#The logic for a template vs project transfer the top command is template

if [ $transfernum = "y" ]

     then

echo "List the files that you would like transfer. Be sure to"
echo "give the management ID ex /ci/r34 /ci/ty5 and so on.: \c"
read filetrans

     /opt/vignette/vignette/5.5/bin/solaris/transferproject -b $exportmach:18100:$prjnm -o export -t "$filetrans" -p

/var/opt/vignette/$bkpnm -l $usernm:$passwrd;

     else

     /opt/vignette/vignette/5.5/bin/solaris/transferproject -b $exportmach:18100:$prjnm -o export -p

/var/opt/vignette/$bkpnm -l $usernm:$passwrd;

fi
 


#############################################################################################################################


#This is the backup of the Target Server

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b $importmach:18100:$pardir -o export -p /var/opt/vignette/$tbkpnm -l

username:password;




#This is the import of the Targer Server this needs to change to utilize the "z" flag

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b $importmach:18100:$pardir -o import -p /var/opt/vignette/$bkpnm -l

username:password;


echo "Vignette Transfer Complete, Use Vignette Tools to Varify Success"
0
 

Author Comment

by:liam_2e
Comment Utility
Ok why does this not work?? I thought this would be the easiest way to write this script but it does not work?? And I  dont know why! I can read the file the id #s are correct the script does run?


###########################################################
# Vignette Project Transfer Script computer1 to  computer2                                               #                
#                                                         #
# NetWeb                                                  #
###########################################################

#!/usr/bin/ksh

echo "Vignette Project/Template/Data Transfer Script"


#############################################################################################################################


IFS=":"
while read projexpmgtid parprojinpmgtid exportnm
do

## do your stuff here


#############################################################################################################################

#The project export

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b computer1:18100:$projexpmgtid -o export -p /var/opt/vignette/$exportnm -l $admin:password1;

#############################################################################################################################

#This is the import

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b computer2:18100:$parprojinpmgtid -o import -p /var/opt/vignette/$exportnm -l admin:password2;


done < /export/home/vigna/Scripts/vgninput.txt
more /export/home/vigna/Scripts/vgninput.txt

echo "Vignette Transfer Complete, Use Vignette Tools to Varify Success"

vnginput.txt has this in it
/pr/3rd:/pr/76y:exporttest


0
 

Author Comment

by:liam_2e
Comment Utility
Ok here is where I am now and it is still not working. But I think I am getting closer!
I think my problem is that my variables are not being initialized?? This is what my text file that
contains my varibles looks like.
vnginput.txt has this in it
/pr/3rd:/pr/76y:exporttest


###########################################################
# Vignette Project Transfer Script computer1 to computer2 #                    
#                                                         #
#                                                         #
#                                                         #
###########################################################

#!ksh

echo "Vignette Project/Template/Data Transfer Script"

sleep 1;

echo " "

################################################################################


IFS=":"
while (read projexpmgtid; read parprojinpmgtid; read exportnm)
do

## do your stuff here

################################################################################
 

#The project export

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b computer1:18100:$projex
pmgtid -o export -p /var/opt/vignette/$exportnm -l $admin:password1;

#allow thirty seconds for export to complete
sleep 30;

################################################################################

#This is the import

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b computer2:18100:$parpro
jinpmgtid -o import -p /var/opt/vignette/$exportnm -l admin:password2;


done < /export/home/vigna/Scripts/vgninput.txt
 
more /export/home/vigna/Scripts/vgninput.txt
echo "Vignette Transfer Complete, Use Vignette Tools to Varify Success"
###############NOTE I PUT THIS PIECE IN TO SEE IF MY VARIABLES ARE BEING CREATED
###############AND I DON'T BELIEVE THAT THEY ARE NOTHING IS BEING PRINTED
print $projexpmgtid
print $parprojinpmgtid
print $exportnm
0
 

Author Comment

by:liam_2e
Comment Utility
Ok Here is where I am at now. I have been putting in echo statements to see how far the script gets. It runs as far as "IFS all done" so there must be some problem with my while statement??
Thanks
Liam
###########################################################
# Vignette Project Transfer Script devsx022 to devsx025   #                
# Created by Liam Tuohey ltuohey@massmutual.com 6/29/01   #
# NetWeb                                                  #
###########################################################

#!/bin/ksh

echo "Vignette Project/Template/Data Transfer Script Starting"


#############################################################################################################################

VPT_SRC="/export/home/vigna/Scripts/vgninput.txt"
echo "VPT_SRC variable made"
IFS=":"
echo "IFS all done"
while read line
echo "variables created"
do
echo "do start"
projexpmgtid=$(echo $VPT_SRC | awk -F: '{print $1}')  #the first field
parprojinpmgtid=$(echo $VPT_SRC | awk -F: '{print $2}')  #the second one
exportnm=$(echo $VPT_SRC | awk -F: '{print $3}')  #the third one

echo "variable values set"
#############################################################################################################################

#The project export

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b devsx022:18100:$projexpmgtid -o export -p /var/opt/vignette/$exportnm -l admin:easadmin;

echo "export complete"

sleep 30;

#############################################################################################################################

#This is the import

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b devsx025:18100:$parprojinpmgtid -o import -p /var/opt/vignette/$exportnm -l admin:xsw21qaz;
echo "import complete"

done

exit 0
0
 

Author Comment

by:liam_2e
Comment Utility
Ok Here is where I am at now. I have been putting in echo statements to see how far the script gets. It runs as far as "IFS all done" so there must be some problem with my while statement??
Thanks
Liam
###########################################################
# Vignette Project Transfer Script devsx022 to devsx025   #                
# Created by Liam 6/29/01   #
# NetWeb                                                  #
###########################################################

#!/bin/ksh

echo "Vignette Project/Template/Data Transfer Script Starting"


#############################################################################################################################

VPT_SRC="/export/home/vigna/Scripts/vgninput.txt"
echo "VPT_SRC variable made"
IFS=":"
echo "IFS all done"
while read line
echo "variables created"
do
echo "do start"
projexpmgtid=$(echo $VPT_SRC | awk -F: '{print $1}')  #the first field
parprojinpmgtid=$(echo $VPT_SRC | awk -F: '{print $2}')  #the second one
exportnm=$(echo $VPT_SRC | awk -F: '{print $3}')  #the third one

echo "variable values set"
#############################################################################################################################

#The project export

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b devsx022:18100:$projexpmgtid -o export -p /var/opt/vignette/$exportnm -l admin:easadmin;

echo "export complete"

sleep 30;

#############################################################################################################################

#This is the import

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b devsx025:18100:$parprojinpmgtid -o import -p /var/opt/vignette/$exportnm -l admin:xsw21qaz;
echo "import complete"

done

exit 0
0
 

Author Comment

by:liam_2e
Comment Utility
###########################################################
# Vignette Project Transfer Script comp1 to comp2   #                
# Created by Liam Tuohey  6/29/01   #
# NetWeb                                                  #
###########################################################

#!/bin/ksh

echo "Vignette Project/Template/Data Transfer Script Starting"


#############################################################################################################################

VPT_SRC="/export/home/vigna/Scripts/vgninput.txt"
echo "VPT_SRC variable made"
IFS=":"
echo "IFS all done"
while read line
echo "variables created"
do
echo "do start"
projexpmgtid=$(echo $VPT_SRC | awk -F: '{print $1}')  #the first field
parprojinpmgtid=$(echo $VPT_SRC | awk -F: '{print $2}')  #the second one
exportnm=$(echo $VPT_SRC | awk -F: '{print $3}')  #the third one

echo "variable values set"
#############################################################################################################################

#The project export

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b comp1:18100:$projexpmgtid -o export -p /var/opt/vignette/$exportnm -l admin:4436645;

echo "export complete"

sleep 30;

#############################################################################################################################

#This is the import

/opt/vignette/vignette/5.5/bin/solaris/transferproject -b comp2:18100:$parprojinpmgtid -o import -p /var/opt/vignette/$exportnm -l admin:543673d;
echo "import complete"

done

exit 0
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now