LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2009, 10:53 AM   #1
D4ve G
Member
 
Registered: Jun 2005
Location: Canada
Distribution: XP / Ubuntu
Posts: 55

Rep: Reputation: 15
Shell script?


How to I tell in a shell script if a command is valid or not?

For example if the user typed

./MyScript ls

How could I check 'ls' if ls existed or not?
 
Old 02-14-2009, 11:19 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
The test for "exists" is "-e" like in: '[ -e "$1" ] || echo {doesnt exist.; exit 1; }'.
 
Old 02-14-2009, 11:29 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
better is
Code:
if [ -x file ]
which indicates that the file exists but that it is also an executable file.

(That wasn't that hard to find in Google!)

jlinkels
 
Old 02-14-2009, 11:34 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The argument must have the full path of the executable, otherwise the test fails. Better to use the which command.
 
Old 02-15-2009, 09:56 AM   #5
D4ve G
Member
 
Registered: Jun 2005
Location: Canada
Distribution: XP / Ubuntu
Posts: 55

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
The argument must have the full path of the executable, otherwise the test fails. Better to use the which command.
I got it working for some some commands but not all.
Which sometimes only returns the path but sometimes it returns more.

Example:
ls: aliased to /usr/bin/ls -CF
 
Old 02-15-2009, 10:07 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by D4ve G View Post
Which sometimes only returns the path but sometimes it returns more.
See 'which --skip-alias --skip-functions' ?
 
Old 02-15-2009, 10:18 AM   #7
D4ve G
Member
 
Registered: Jun 2005
Location: Canada
Distribution: XP / Ubuntu
Posts: 55

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by unSpawn View Post
See 'which --skip-alias --skip-functions' ?
Not sure if I'm stupid or what but I can't find any flags for which.
 
Old 02-15-2009, 10:39 AM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
If you want to use 'which', use the last function result: 0 for one or more executables found, 1, 2: no executable found.

Code:
donald_pc:~$ which bash
/bin/bash
donald_pc:~$ echo $?
0
donald_pc:~$ which pietje
donald_pc:~$ echo $?
1
jlinkels
 
Old 02-15-2009, 12:18 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by D4ve G View Post
Not sure if I'm stupid or what but I can't find any flags for which.
Have you tried them? Did you receive some error about invalid option? Anyway, I would stick with jlinkels' solution:
Code:
if which $command > /dev/null 2>&1
then
  echo "Command $command exists!"
fi
And... Happy Birthday to you!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration