LinuxQuestions.org
Visit Jeremy's Blog.
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 04-20-2009, 05:57 PM   #1
v333k
Member
 
Registered: Apr 2004
Location: 127.0.0.1
Posts: 38

Rep: Reputation: 15
check if directory exists using shell script


hi everyone,
I am trying to write some small script file that will check if a USB stick is connected to my pc or not. I can't seem to get it to work, but I am sure it is a very simple fix.

here is what I have in my file:
Code:
#!/bin/bash

usb_path = "/media/disk/"

if[ -f $usb_path ]
then
    echo "path exists"
else
    echo "path does not exist"
fi
I named my file as: check_usb.sh
and I changed permissions: chmod 755 check_usb.sh

The error I keep getting is this:
# ./check_usb
./check_usb.sh: line 3: usb_path: command not found
path exists

I am running on Fedora core 10.
Can anyone help me figure out what the problem is?

If I run these lines through console it works fine (meaning, I type the commands in this file straight in the command line/console).

thanks in advance.
 
Old 04-20-2009, 06:13 PM   #2
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
It's the spaces around '='. Try

Code:
usb_path="/media/disk/"
also, you should have a space between your if and the '['.

Last edited by GazL; 04-20-2009 at 06:16 PM.
 
Old 04-20-2009, 06:14 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Two small problems. Linux shell-scripts are very fussy about spacing in certain places:
Code:
usb_path = "/media/disk/"
There must not be any spaces around the assignment operator (=)
Code:
if[ -f $usb_path ]
There must be spaces around the square brackets.

This is really for the same reasons. A command is separated from its arguments by (a) space(s), so usb_path is seen as a command and not an assignment, while the [ ... ] construction is actually a command (it's equivalent to test ...) so needs the spaces.

Hope this helps,
Rob
 
Old 04-20-2009, 06:25 PM   #4
v333k
Member
 
Registered: Apr 2004
Location: 127.0.0.1
Posts: 38

Original Poster
Rep: Reputation: 15
PERFECT!

Thanks a lot!!!!
 
Old 04-20-2009, 07:27 PM   #5
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Another thought just occurred - if you're wanting to check that $usb_path exists, and it is a directory, you'd need:
Code:
if [ -d $usb_path ]
... as -f tests if something is a normal file (i.e. not a directory, block device, etc). If you just want to test that it exists (and don't care what sort of file it is), you could use:
Code:
if [ -e $usb_path ]
 
Old 04-21-2009, 09:59 AM   #6
v333k
Member
 
Registered: Apr 2004
Location: 127.0.0.1
Posts: 38

Original Poster
Rep: Reputation: 15
GREAT! Thanks for the tips!
At the moment, I want to check if the usb is connected to my PC - and I don't really care about other parts; therefore, it looks like using -e will be my best choice.

Thanks again!

Quote:
Originally Posted by Robhogg View Post
Another thought just occurred - if you're wanting to check that $usb_path exists, and it is a directory, you'd need:
Code:
if [ -d $usb_path ]
... as -f tests if something is a normal file (i.e. not a directory, block device, etc). If you just want to test that it exists (and don't care what sort of file it is), you could use:
Code:
if [ -e $usb_path ]
 
Old 04-23-2009, 06:31 AM   #7
jisjis
LQ Newbie
 
Registered: Apr 2009
Posts: 18

Rep: Reputation: 0
Quote:
Originally Posted by GazL View Post
It's the spaces around '='. Try

Code:
usb_path="/media/disk/"
also, you should have a space between your if and the '['.

Linux is so finicky !!!


Linux

Last edited by jisjis; 04-26-2009 at 04:43 AM.
 
Old 04-23-2009, 07:21 AM   #8
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
Quote:
Originally Posted by jisjis View Post
Linux is so finicky !!!
Ever tried C programming ?
 
Old 04-23-2009, 09:09 AM   #9
v333k
Member
 
Registered: Apr 2004
Location: 127.0.0.1
Posts: 38

Original Poster
Rep: Reputation: 15
Yes - actually, I am writing my socket server in C, but there are certain things that I would like to run in shell from a C program.
For example, it will be much faster (in my opinion) to obtain the directory list by running a shell script (ls -la) instead of writing a C function that will try to parse the directory, etc... etc... etc....

It is also nice to try something new and not go back to known methods of implementation!

Just a clarification - that wasn't me who said Linux is finiky!

In any case... thanks for your suggestion!
 
Old 04-23-2009, 09:29 AM   #10
Libu
Member
 
Registered: Oct 2003
Location: Chennai
Distribution: Slackware 12.1
Posts: 165

Rep: Reputation: 36
Quote:
Originally Posted by jisjis View Post
Linux is so finicky !!!
Well its not linux, its bash and you can use perl if you want to !
 
  


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
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
Find if a directory exists in a gawk script duparcmeur Linux - Newbie 2 04-02-2008 01:57 PM
Testing for member exists in shell script hukcjv Linux - Newbie 5 03-22-2008 09:35 AM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM

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

All times are GMT -5. The time now is 07:26 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
Open Source Consulting | Domain Registration