Replacement of exit function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacement of exit function
# 1  
Old 12-26-2011
Replacement of exit function

I am reading multiple numbers from user and getting its respective string value from the One.txt.But while putting this text value to the two.txt file , i want to check that if this string already there in the file , if the string already exists in the two.txt file .....the string will be ignored. and if the string is not exists in a two.txt file then it will be appended to the two.txt file.

For the above problem i am reading values 1,2,3 from the screen, but while getting value from one file and putting values to another file. This process continues for only 1 value, i am using exit function in the file.my code is here below.

Code:
echo "ENter Permission values seprated by commas"
read ch
#replace commas by spaces 
list=`echo $ch | tr ',' ' ' `
for val in ${list} #check till the permsiion choices entered
do
    temp=0
    command=`awk 'BEGIN {FS="|"}{print $2}' g1.txt` # get all the permssions names seprated by commas
    my=`echo $command | cut -d "," -f$val`         

    while read ln
    do
          ct=`echo $ln | cut -d "," -f1`        
          # echo $ct
        if [ $ct = $my ] ;then
            echo "permission already given" 
            temp=0            # If permission applied temp set to 0
            exit
        fi    
    done < g2.txt

    if [ temp != 0 ] ;then
        echo "$my," >>  g2.txt
        break
    fi
done

Sample Input file g1.txt
Code:
1|HideCDrive,    | : REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v NoDrives /t REG_DWORD /d 4 /f ,
2|HideRunButton, | : REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v NoClose /t REG_DWORD /d 1 /f ,
3|DisableControlPanel, | : REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v NoDispCPL /t REG_DWORD /d 1 /f ,
4|AddMycomputerIexploreandMynetworkplaceMyDocumentIcononDesktop, | : REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explore\HideDesktopIcons\NewStartPanel" /v {20D04FE0-3AEA-1069-A2D8-08002B30309D} /t REG_DWORD /d 0 /f ,

Sample output file
Code:
DisableControlPanel,
HideRunButton,
AddMycomputerIexploreandMynetworkplaceMyDocumentIcononDesktop,
Iconscolor,


Last edited by Franklin52; 12-26-2011 at 10:33 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 12-26-2011
Try replacing 'exit' with 'break'. And 'break' in last if-condition is not required. You may remove it. Also, please provide a sample input and required output. It'll help us understand the problem better.

---------- Post updated at 12:40 ---------- Previous update was at 12:12 ----------

Try this small 2-line script. It does same as initial code posted in post #1:

Code:
[root@hostname test]# cat test.sh
[ ! -e g2.txt ] && touch g1.txt
for val in $* ; do grep "^$val" g1.txt | cut -d'|' -f2 | tr -d , >> g2.txt; done

Instead of reading from user, enter the values as positional parameters.
Code:
[root@hostname test]# bash test.sh 4 1 2
[root@hostname test]
[root@hostname test]
[root@hostname test]# cat g2.txt
AddMycomputerIexploreandMynetworkplaceMyDocumentIcononDesktop
HideCDrive
HideRunButton
[root@hostname test]
[root@hostname test]

This User Gave Thanks to balajesuri For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Programming

exit function doubts

At many places in program I see exit() being called with 2 passed as parameter to it. exit(2); What does this 2 stands for? Can I get a list all such parameters to exit? (6 Replies)
Discussion started by: rupeshkp728
6 Replies

3. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

4. UNIX for Advanced & Expert Users

Cannot exit from a function?

Hi, Is there a way to exit from a subcommand, which is a function in my example below? #!/bin/ksh function exitFunction { if ]; then echo "success" elif ]; then echo "failed" exit 1 # the exit problem fi exit 0 } ... (2 Replies)
Discussion started by: chstr_14
2 Replies

5. Shell Programming and Scripting

Exit from function

Hi, I just want to exit from function if some condition doesnt meet then control should go back to main program and start running from where it left.. When i used "exit" inside the function, its simply exited from entire script and it didnt run anymore.. Any idea how to do this.. Thanks... (3 Replies)
Discussion started by: nram_krishna@ya
3 Replies

6. Shell Programming and Scripting

exit from function

Hi all, My kshell code is not working, when I use a function to return something. But when I use the same function as without returning any values, it is working. Pls help me here. Code1 : func1 () { y=`echo $x | grep XXX| cut -f 2 -d ' '` if ; then exit 100 ... (2 Replies)
Discussion started by: poova
2 Replies

7. Programming

capturing enter and exit of every function

If I have a "Hello World" function (just prints that) and a similar "Goodbye World" function... is there a way (maybe a compiler option?) that I can get them to be executed directly as the absolute first and last things run in every function call. So for example, the following code: int foo()... (5 Replies)
Discussion started by: jjinno
5 Replies

8. UNIX for Dummies Questions & Answers

EXIT from a Program not from the function..

Hi, I want a command in unix shell script which will exit my whole program, not only from the function it's using. For e.g: $1 == "m_who" && $4 == "Extrnl Vendor" { print "You don have access" exit (0); } If this error is showing on the screen, then nothing should not... (9 Replies)
Discussion started by: ronix007
9 Replies

9. Shell Programming and Scripting

exit shell script from function

Hi all, I have tried to put a exit 0 statement within a function I have created in the shell script but it doesn't seem to exit out of the script? Can someone tell me why? And is there any other way to exit out of the script from within a function? Thanks! (1 Reply)
Discussion started by: nvrh7
1 Replies

10. Programming

How to get exit value of an executable that gets called from function?

How to get exit value of an executable that gets called from function? I have an executable called “myexec” which returns 0 on success and different values for different fail scenarios. I need to call this (myexec) executable from “myprog()” function of other executable and get the exit value... (1 Reply)
Discussion started by: sureshreddi_ps
1 Replies
Login or Register to Ask a Question