LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2009, 07:08 AM   #1
karuna-bdc
LQ Newbie
 
Registered: Oct 2008
Location: Nilai, Malaysia
Distribution: Ubuntu
Posts: 17

Rep: Reputation: 0
using if in bash script to check if prog exists


i have this script i wrote to use in nautilus to play mp3s in mplayer just for shell script practice, and it dosen't seem to work if i put an if routine in it to check that mplayer is executable and exists.

#!/bin/bash

if [-x mplayer] then
mplayer $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
fi

but if i take out the test:

#!/bin/bash

mplayer $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS


It works...
what am i doing wrong with the if check?
 
Old 06-21-2009, 07:20 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
You can do:

Code:
if which mplayer
then
  mplayer $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
fi
 
Old 06-21-2009, 07:25 AM   #3
karuna-bdc
LQ Newbie
 
Registered: Oct 2008
Location: Nilai, Malaysia
Distribution: Ubuntu
Posts: 17

Original Poster
Rep: Reputation: 0
That works, thanks

but what was wrong with the previous code?
 
Old 06-21-2009, 07:36 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Well, you need the full path to mplayer. If you run 'which mplayer' it will give you the full path.

if [-x /usr/bin/mplayer] then
 
Old 06-21-2009, 07:38 AM   #5
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 karuna-bdc View Post
but what was wrong with the previous code?
If you do if [ -x mplayer ] you look for an executable just in the current directory, whereas the which command tells if you have the executable in any directory included in your PATH.
 
Old 06-21-2009, 01:37 PM   #6
karuna-bdc
LQ Newbie
 
Registered: Oct 2008
Location: Nilai, Malaysia
Distribution: Ubuntu
Posts: 17

Original Poster
Rep: Reputation: 0
ahhh, that makes sense. Thanks
thats one of the wonderful things about learning linux - the community is loadsa help
 
Old 06-21-2009, 07:23 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
NB in your orig code, you had no spaces around the '[', ']'. That would be a syntax error.
 
  


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
check if directory exists using shell script v333k Programming 9 04-23-2009 09:29 AM
Bash Help: Check if file exists richinsc Programming 6 01-09-2009 12:27 PM
bash check folder exists zerocool22 Programming 22 06-01-2008 07:53 AM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM
Does anyone know of a bash script that will determine if RPM exists on a system? jimwelc Linux - Software 3 12-28-2004 03:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:31 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