To see if a program exist and is executable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To see if a program exist and is executable
# 1  
Old 09-28-2010
Hammer & Screwdriver To see if a program exist and is executable

Hi, I am making a program that needs to detect if the program name in parameter is a valid runable program.
But the line if [ -x ${1} ]; then never seem to work.
Even if I run like:
Code:
./script cat "-u" cat "-u" inputfile

Thank you everyone.


Code:
#!/bin/bash
#
usage() { 		#print usage message and quit
	echo "Usage: program1 [program1 options] program2 [program2 options] [inputfilelist]"
	exit 1
}

program1=${1}
program2=${3}
program1setting=${2}
program2setting=${4}

if [ -x ${1} ]; then
	echo "program 1 exist"
else
	echo "first program does not exist"
	usage
fi

if [ -x ${3} ]; then
	echo "program 2 exist"
else
	echo "second program does not exist"
	usage
fi


Last edited by DukeNuke2; 09-28-2010 at 06:44 AM..
# 2  
Old 09-28-2010
This is because "-x" requires a path to the executable. In your example you are calling it with the parameter "cat" which probably doesnt exist in the current dir, but probably does exist in your path somewhere. You could change the "if" statements to use the command "which" that looks throughout your path for the executable:
Code:
if [ -x "`which ${1}`" ]; then

This User Gave Thanks to citaylor For This Post:
# 3  
Old 09-28-2010
thank you!

Last edited by leonmerc; 09-28-2010 at 08:36 AM.. Reason: avoid farther misunderstanding
# 4  
Old 09-28-2010
For future reference, homework and coursework questions can only be posted in this forum under special homework rules.
# 5  
Old 09-28-2010
Quote:
Originally Posted by pludi
For future reference, homework and coursework questions can only be posted in this forum under special homework rules.
Thanks for the warning, but the question I asked has nothing directly to do with the homework I was assigned. My homework questions are in much bigger scale, there is absolutely no possibility of someone one else doing my work or anything like that. I was simply asking very specific technical questions.
I wished i didnt mention it but hey, can't blame a man for being honest.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

3. Programming

Makefile for more than 1 executable program

Can anyone give me a makefile that creates 3 exe?for example, let's suppose i have the following files: blah1.c blah1.h blah2.c blah2.h blah3.c blah3.h i've searched and searched but so far i was not able to complete it. (4 Replies)
Discussion started by: bashuser2
4 Replies

4. Programming

Call a C programming executable inside a C program

hi guys i have only basic knowledge of C so guys plz help me ..... is C language support call the C executable inside the C ?? example contect mainA.c have a many function define in the struct,when i compile mainA and make a executable the name is ( A ),can i use executable C inside the C... (5 Replies)
Discussion started by: isnoname
5 Replies

5. Programming

how to call c executable inside c program??

hi guys i have only basic knowledge of c so guys plz help me ..... i want 2 call c executable which requires file name as argument and i need to modify file contents before calling that executable now my question is how can i call this c executable inside another c program with arguments ?? i... (9 Replies)
Discussion started by: zedex
9 Replies

6. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

7. Programming

zombie to exist after the termination of main program..

main() { pid_t child; child=fork(); if(child > 0) {sleep(60); } else {exit(0); } } the above code will create zombie process,which will be adopted by init as soon as parent process will dies.Can any one gimme a code or an alogrithm to keep this zombie proocess alive even after... (6 Replies)
Discussion started by: anilchowdhury
6 Replies

8. Programming

problem in creating executable for a client program

Hi, I am trying to run simple client server c program in unix.At the compling stage server is creating an executable but the client is not. below is the link to the source codes: http://www.cs.rpi.edu/courses/sysprog/sockets/server.c http://www.cs.rpi.edu/courses/sysprog/sockets/client.c ... (2 Replies)
Discussion started by: konas
2 Replies

9. Programming

launch an executable from a C++ program

Hi everybody! Could you please tell me how can I launch an executable from a C++ (on unix) program? thanks in advance! (2 Replies)
Discussion started by: nadiamihu
2 Replies

10. UNIX for Dummies Questions & Answers

TK executable?

hi All, Can any one help me to find out a solution for creating an Executable file for a TK widget script. Is there any command line option for it. I am working in HP Unix workstation. Regards, Anent (3 Replies)
Discussion started by: anent
3 Replies
Login or Register to Ask a Question