Question

To start learning BASH scripting, I've created a trivial script that curls down a stock price from YAHOO and prints it to STDOUT. I've set the permissions to rwx for everyone, and moved the file into my path:

Script:

root@raspberrypi:~/code/scripts$ cat quote
#!/bin/bash

while getopts "s:" opt; do
    case $opt in
        s)STOCK="$OPTARG";;
        \?) echo "ERROR"; exit 1;;
    esac
done

PRICE=$(curl -s "http://download.finance.yahoo.com/d/quotes.csv?s=${STOCK}&f=l1")
echo "${STOCK}: ${PRICE}"; exit 0

exit 0

I then set the permissions for all users:

root@raspberrypi:~/code/scripts$ chmod 777 quote

Here is my $PATH:

root@raspberrypi:~/code/scripts$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

I now move it into my path in what I read was the appropriate location for custom user scripts:

root@raspberrypi:~/code/scripts$ ls -la /usr/local/bin
total 12K
drwxrwsr-x  2 root staff 4.0K Apr 30 01:28 ./
drwxrwsr-x 10 root staff 4.0K Jan  1  1970 ../
-rwxrwxrwx  1 root root   433 Apr 30 01:22 quote*

The which command will find it (expected):

root@raspberrypi:~/code/scripts$ which quote
/usr/local/bin/quote

PROBLEM: when I run the script, it returns the first option on the next line followed by my prompt:

root@raspberrypi:~/code/scripts$ quote -s aapl
'-s'root@raspberrypi:~/code/scripts$

But, when I run the script with a full path, it works just fine:

root@raspberrypi:~/code/scripts$ /usr/local/bin/quote -s aapl
-s: 592.33

Apologies if this less a programming question and more a Unix question, but I want to make sure I rule out a problem with my code before I do anything else.

I'm sure this is something very easy, so thanks in advance for the extra set of eyes.

Était-ce utile?

La solution

You just chose an unfortunate name for your script. quote is also the name of a function from the bash_completion package, and functions override external scripts.

type quote will show that it's a function (whereis doesn't know about these things).

Either rename it or disable bash_completion. Or run command quote every time.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top