LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 08: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, 08:20 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
You can do:

Code:
if which mplayer
then
  mplayer $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
fi
 
Old 06-21-2009, 08: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, 08:36 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
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, 08:38 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 02: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, 08:23 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 10:29 AM
Bash Help: Check if file exists richinsc Programming 6 01-09-2009 01:27 PM
bash check folder exists zerocool22 Programming 22 06-01-2008 08:53 AM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 04: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 04:01 PM


All times are GMT -5. The time now is 07:29 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration