Sponsored Content
Full Discussion: String comparison problems
Top Forums Shell Programming and Scripting String comparison problems Post 302601731 by as234301 on Friday 24th of February 2012 09:21:24 AM
Old 02-24-2012
String comparison problems

Req:
===========
1)I have one shell script a.sh
2)I opened Putty session , with user name = SIR100 , and i ran a.sh script in back ground and
it took 5 minutes to complete.Meanwhile,i ran same script second time i.e a.sh with same user it sholud not allow to run the script , but it's allowing using my below code.But, if i ran the same script , with different user , i.e SIR200 , it should allow to run the script.
3)Final requirement is , One user can't run the script , untill first one has completed.

4)According to below code , when user runs first time , cursor shoud goes to last else block , and script needs to be started.Where in , if same user run second time before completion of first scrit , it should goes to last if condition , and script should get exit.

5)Here , main problem is , how to compare 2nd time if the same user is running the script.

below is the code snippet:
==============================
Code:
#!/bin/sh

#Below command will print the login user name
oper=`whoami`



#Below command will print , on which user the script a.sh is running.
User=`ps -ef|grep a.sh|grep -v grep|awk '{print $1}'`

echo "Login User " $oper
echo "User Name  " $User

flag=0

listOfUsers=${echo $User | tr " " "\n"}
echo "List of Users " $listOfUsers

for userName in $listOfUsers
do
    echo $userName  
    if [ "${userName}" = "${oper}" ]
    then
    echo "Inside if aaaaaaa"
        flag=1
        exit 1
    fi
done


echo "flag value  " $flag 

if [ $flag -eq 1 ]
 then
 echo "Inside if bbbbbbbbb"
        echo "script is in progress..."
	
        exit 1
 else
        echo "Script is not running it initiated now."
	sleep 40
 fi


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 02-24-2012 at 10:31 AM.. Reason: Please use code tags for code and data samples, thank you
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

string comparison

The script will read a bunch of names, and test if it contains "John", but as below apparently ~ does not work, so what is the easiest way to perform string comparison in bash shell script? thanks ... elif then echo "get John" .... (2 Replies)
Discussion started by: fedora
2 Replies

2. Shell Programming and Scripting

string comparison

Hello experts, (tcsh shell) Quite new to shell scripting... I have got a file with a single word on each line. Want to be able to make a comparison such that i can read pairs of words that are ROT13 to each other. Also, i would like to print the pairs to another file. Any help... (5 Replies)
Discussion started by: Jatsui
5 Replies

3. Shell Programming and Scripting

Help with String Comparison

I'm running the following script to compare string values to a regexp: for entry in $(lpinfo -v | cut -c 1-); do if then echo "blah" continue fi done Whenever I run it, each token of lpinfo is being interpreted as a command and I get errors such as: ... (2 Replies)
Discussion started by: hypnotic_meat
2 Replies

4. Shell Programming and Scripting

String comparison

Hi, I have the below logic. Here 'X' is a variable having some string. if then echo "i dont need to go to ofc" else echo "i need to go to ofc" Please help me to write it in unix. Thanks. (2 Replies)
Discussion started by: 46019
2 Replies

5. Shell Programming and Scripting

Help with string comparison

#!/bin/sh PRINTF=/usr/bin/printf MACHINE_NAME=`uname -n` TIME=`date +"%H"` $PRINTF "Welcome to $MACHINE_NAME. What is your name?\n" read NAME if ; then $PRINTF "Good morning $NAME, how are you?\n" elif ; then $PRINTF "Good afternoon $NAME, how are you?\n" else $PRINTF "Good... (2 Replies)
Discussion started by: ikeQ
2 Replies

6. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

8. Shell Programming and Scripting

String comparison

hi team, i want to compare the below string from logs, but its is not working. if ]; then echo "restart some process" fi (4 Replies)
Discussion started by: mfaizan40
4 Replies

9. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

10. Shell Programming and Scripting

File comparison problems

We are running parallel reports with Legacy vs a new system and want to compare certain lines with the diff or diff -b command. When a six digit number is read at the beginning of the line (no decimal point) we read and ignore a header line (next), move any minus sign to the end of the number and,... (7 Replies)
Discussion started by: wbport
7 Replies
script(1)						      General Commands Manual							 script(1)

NAME
script - Makes a transcript of terminal session SYNOPSIS
script [-a] [file] The script command makes a transcript of everything printed on your terminal. OPTIONS
Appends the transcript to file rather than writing it to file. OPERANDS
The name of an output file that will contain the transcript of the session. If this parameter is omitted, the file typescript is written. DESCRIPTION
The transcript is written to file, or appended to file if the -a option is given. If no file name is given, the transcript is saved in the file typescript. The script ends when the forked shell exits. This program is useful when you are using a CRT and want a hard-copy record of the dialog (for example, a technical writer might create an example of a working session this way). If you specify the -a option and the file does not exist, it is created. If you do not specify the -a option and the file exists, it is replaced. RESTRICTIONS
The script command requires a streams based terminal. In single user mode, streams may not be enabled. Under these circumstances, script will exit with no action. If you are the superuser and need to run this command in single user mode, use the following special instruc- tions. Enabling Streams If it is necessary to enable a streams environment in the single user mode, enter the command /sbin/init.d/streams. This command is avail- able to the superuser only. SEE ALSO
Commands: autopush(8), cat(1), echo(1), strsetup(8), tee(1) System Administration script(1)
All times are GMT -4. The time now is 11:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy