#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    22
    Rep Power
    0

    How to pass parameters to programs in shell scripts?


    Hi everybody,

    I'm trying to create a simple script to automate a few programs I wrote on my raspberry pi, but I can't pass arguments to the programs inside a script. Obviously I'm doing something wrong, but I can't figure out what.

    When I execute my programs, I use:
    Code:
    mono Software.exe /mnt/nas/path-to-process/
    or
    Code:
    ./Software.exe /mnt/nas/path-to-process/
    When I create a script and add either line above, I get:
    Code:
     does not existnas/path-to-process
    Note the space in front, and the missing "/mnt/" - I figured I needed to escape the forward slashes, but that didn't work.
    Using single quotes did not work, neither did double quotes. Assigning the path to a variable foo and using Software.exe $foo didn't work either...

    Can anybody please inform me how I can pass a path to my program inside a script? I am NOT trying to pass the arguments to the script - everything should be inside the script.

    Thanks!

    Marlon
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,638
    Rep Power
    1821
    What shell are you using at the command line? How are you running the shell script, and what does it contain? I have never used a pi, but from what I have done with *ix I can think of no reason why anything is monkeying with command being executed.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    22
    Rep Power
    0
    I'm using the bash shell on my raspberry pi, as verified by the following command:
    Code:
    ps -p $$
    this lists bash under the header CMD
    The shell script I wrote, contains the following:
    Code:
    mono Software.exe /mnt/nas/path-to-process/
    mono being the mono environment, Software.exe my C# program, /mnt/nas being my mounted nas, and path-to-process being a temporary directory to test

    If I execute the above line by typing it in the shell, everything is fine.

    I execute the shell script (named script.sh, permissions 700) by typing
    Code:
    ./script.sh
    Thanks!

    Marlon
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,638
    Rep Power
    1821
    Things to try, hopefully of some help but I fear with it being a 'one-liner' they will be of limited use!
    For the first line of the script use a shebang line: #! /path/to/bash_shell to ensure it is interpreted correctly. Add a sh -x as the second line to turn on debugging, and see what that does.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    937
    Rep Power
    280
    what happens if you use the full paths for Mono and for your Software.exe in your script?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,638
    Rep Power
    1821
    Originally Posted by DonR
    what happens if you use the full paths for Mono and for your Software.exe in your script?
    That was going to be suggested next ...
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    22
    Rep Power
    0
    Originally Posted by SimonJM
    Things to try, hopefully of some help but I fear with it being a 'one-liner' they will be of limited use!
    For the first line of the script use a shebang line: #! /path/to/bash_shell to ensure it is interpreted correctly. Add a sh -x as the second line to turn on debugging, and see what that does.
    Thanks for the suggestions, I tried them and now my script looks like this:
    Code:
    #! /bin/sh
    sh -x
    /usr/bin/mono /home/pi/hunter/Verifier.exe /mnt/nas/path-to-process/
    If I run this script, I get the error:
    -bash: ./verify.sh: /bin/sh^M: invalid interpreter: file or path does not exist

    Running the script with only the last line, resulted in a similar error as before.

    Any thoughts?
  14. #8
  15. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,557
    Rep Power
    4549
    Is "/path-to-process/" the actual name of the target directory? It sounds to me like something you'd see in some example code.
    ======
    Doug G
    ======
    I've never been able to appreciate the sublime arrogance of folks who feel they were put on earth just to save other folks from themselves .." - Donald Hamilton
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,638
    Rep Power
    1821
    That ^M in the error suggests to me that the script (or maybe just that shebang line) was written in a DOS/Windows-based editor and copied over, somehow. You may have a dos2unix command available which should help (check usage first!), or I'd ensure that the script is entered, manually, actually on the pi.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    22
    Rep Power
    0
    Originally Posted by SimonJM
    That ^M in the error suggests to me that the script (or maybe just that shebang line) was written in a DOS/Windows-based editor and copied over, somehow. You may have a dos2unix command available which should help (check usage first!), or I'd ensure that the script is entered, manually, actually on the pi.
    Indeed, I initially created the script on a Windows machine accessing the Pi network share... Don't know how I managed to miss that fact after editing it on the pi!

    Thanks SimonJM!
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,638
    Rep Power
    1821
    Happy to help!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo