Visit Our UNIX and Linux User Community


Shell script validation using process.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script validation using process.
# 1  
Old 02-23-2012
Shell script validation using process.

Hi All,
I have a shell script in Linux and it will be invoked by 2 methods,

1) An automated background process .
2) Logining into the box and directly executing the script.

I need to put a validation in my shell script such that it will be executed successfully on when an automated background process invokes it. When any user login and invoke it manually it should throw an error and exit. Please advise how this can be achieved inside my shell script
# 2  
Old 02-23-2012
Shell logins will have terminal devices, things created by cron and the like won't.

Code:
if /bin/stty -F /dev/tty >/dev/null 2>/dev/null
then
        echo "Cannot run this program from a terminal" >&2
        exit 1
fi

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-23-2012
Thanks a lot for your reply. If I need to validate whether the process is invoked only from a particular parent (i,e automated background parent process p1), then how can I do the validation in the shell script?
# 4  
Old 02-24-2012
How to get that information depends on your system. What is it?
# 5  
Old 02-24-2012
Hi,
Actually, the script is invoked in background from harvest. So, In order to identify whether the script is invoked only from harvest and not in foreground or through anyother background process, I got the $PPID in myscript.ksh, it gives the details as,

myid1 22511 22510 0 02:53 ? 00:00:00 /usr/bin/ksh /tmp/har37sbp6
myid1 23346 22511 02:54 ? 00:00:00 /bin/ksh /home/myid1/myscript.ksh myid1 23366 23346 0 02:54 ? 00:00:00 grep 22511


But the process ("/usr/bin/ksh /tmp/har37sbp6") is forked by a parent harvest process,

myid1 22510 25146 0 02:53 ? 00:00:00 /host1harvest/dstart/harrefreshd 50000 /host1harvest/Harvestr/5

So, In the current shell script(myscrip.ksh) how can I get the parent's parent process name(i,e, 22510, /host1harvest/dstart/harrefreshd 50000 /host1harvest/Harvestr/5) and validate it for harvest string. Please advise.
# 6  
Old 02-24-2012
The Shell way to test if you are in background:
Code:
if [ ! -t 1 ]
then
           echo "In background"
fi

Script tests whether STDOUT is a terminal.


For question 2, please post your exact Operating System and version and what Shell you are using. It also helps to know if the script will be normally invoked by root or another named user.
In the situation where I don't want a script executed by accident I usually call the script with an activation parameter and exit if the correct parameter is not supplied.
This User Gave Thanks to methyl For This Post:
# 7  
Old 02-24-2012
Thanks for your reply.

OS:-
---
Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux

Shell:-
-----
ksh

The script will be invoked a normal user and not by root. Are there any other approaches other than parameters?

Previous Thread | Next Thread
Test Your Knowledge in Computers #862
Difficulty: Medium
Vue uses an HTML-based template syntax that allows binding the rendered DOM to the underlying Vue instance's data.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with validation of URLs in shell script

hello every one I wrote the script to validate all the urls from my server . sourcing text file which contains the list of urls and trying to check every url and based on response printing url is up or not. the issue when i use the below code with url is printing url exist and and in loop it's... (3 Replies)
Discussion started by: markjohn1
3 Replies

2. UNIX for Dummies Questions & Answers

Validation to be added in the shell script

I need help with one of my shell script. The script is working fine but i need to add two condition - i need to get rid of all the below ftp messages and need to have only ftp completed or failed message. example when i run the script i get below lines - Connected to xxxx 220 (vsFTPd... (1 Reply)
Discussion started by: chandraprakash
1 Replies

3. Shell Programming and Scripting

String validation in shell script

Hi All, I am a newbie...I would like to have a function which ll check if a file contains valid strings before "=" operator. Just to give you my requirement: assume my file has content: hello= gsdgsd sfdsg sgdsg sgdgdg world= gggg hhhh iiiii xxxx= pppp ppppp pppp my... (1 Reply)
Discussion started by: rtagarra
1 Replies

4. Shell Programming and Scripting

Validation in shell script.

PICKUPDIR=/home/ready/ DROPDIR=/home/downloaded/ TODAY=$(date '+%d%m%y') LOGFILE=xyz-$TODAY.log ########### #FUNCTIONS# ########### #function to perform file transfer to servercopy folder opalO () { cd $PICKUPDIR for fileName in `ls -1 TES_ONE*` do cp $fileName $DROPDIR done } >>... (4 Replies)
Discussion started by: ravigupta2u
4 Replies

5. Shell Programming and Scripting

Kill a process from parent shell within a shell script

Hi, I am looking for a solution for the following problem: Im Using tcpdump within a shellskript started in a subshell by using brackets: ( /usr/sbin/tcpdump -i ... -c 1 ) - I want the outout of tcpdump saved in a variable - Than tcpdump-Process in the Subshell should be killed - and I... (6 Replies)
Discussion started by: 2retti
6 Replies

6. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

7. Shell Programming and Scripting

EMail Address Validation (Shell Script)

Hi, Can anyone provide me the code snippet for EMail Address Validation. User is going to enter the email address in form window. I need to validate the format is correct. Thanks in Advance BS (3 Replies)
Discussion started by: balajiora
3 Replies

8. Shell Programming and Scripting

shell script data & time validation

How to validate a date and optionly a time in shell scripting when i get the date and time as pararmeters that sent out with the call of the file? (in my case sh union.sh `first parameter ,second parameter...` (4 Replies)
Discussion started by: tal
4 Replies

9. Shell Programming and Scripting

Test File Reading & Validation using Shell script

Please help develop script for below requirement -------Sample file------------------------------- HSVSHOSTRECON 20090115011817BP DARMAR60064966247003504720000000000000000000066626000000000000133000003D003463001332 ... (14 Replies)
Discussion started by: niraj_bhatt
14 Replies

10. Shell Programming and Scripting

Need help in file validation by shell script

Hi I am new to this forum.I need a help in the following: We receve pipe delimited file with transaction ID,tran_date,Quest_cd,Ans_cd,ans_value. Same transaction ID can be repeated with different quest_cd and ans_cd. Basically I need to check if a perticular pair of quest_cd and ans_cd... (1 Reply)
Discussion started by: srichakra
1 Replies

Featured Tech Videos