ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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).
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.
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:
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
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:
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.