Running an executable from bash prompt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running an executable from bash prompt
# 1  
Old 04-22-2013
Running an executable from bash prompt

Hi,

I'm trying to run a program from the bash prompt and I don't understand why it is returning with an error.

Dig is my C program, and it takes in parameters J4, detect, 3 and 0182F98E

Code:
var1="cygdrive/c/2i/test fixture/software/mccdaqtest/debug/Dig J4 detect 3 0182F98E"

when I do

Code:
"$var1"

it gives me a "no such file or directory" error.

what is the correct way to call this program?
# 2  
Old 04-22-2013
Code:
var1="'cygdrive/c/2i/test fixture/software/mccdaqtest/debug/Dig' J4 detect 3 0182F98E"
eval "$var1"

Beware of what goes inside the variable.
# 3  
Old 04-22-2013
Quote:
Originally Posted by oahmad
Code:
var1="cygdrive/c/2i/test fixture/software/mccdaqtest/debug/Dig J4 detect 3 0182F98E"

Why are you storing the command line in a variable? If it's not absolutely necessary, don't do it. If some of the command is dynamically determined, then put only those components in their own, separate variables.

Regards,
Alister
# 4  
Old 04-22-2013
Does the following work from the command prompt:
Code:
cygdrive/c/2i/test fixture/software/mccdaqtest/debug/Dig J4 detect 3 0182F98E

If so, that is the correct way to call the program.

----------------------------
Quote:
it gives me a "no such file or directory" error
It's something to do with cygdrive and fixture relative paths. Try using absolute paths.
# 5  
Old 04-22-2013
Quote:
Originally Posted by hanson44
It's something to do with cygdrive and fixture relative paths. Try using absolute paths.
That fundamental error is that the variable is quoted. Since it's quoted, there is no word splitting performed and the variable's entire contents are treated as the command name.

The following ...
Code:
x='cmd arg1 arg2 arg3'
"$x"

... is equivalent to running ...
Code:
'cmd arg1 arg2 arg3'

... at the prompt. All four words will be treated as the command name.

If the variable is left unquoted, then the space in the pathname will cause a problem. Lose-lose.

Regards,
Alister

Last edited by alister; 04-22-2013 at 03:43 PM..
# 6  
Old 04-23-2013
Guys, thanks for your help. I'm a complete newbie as shell-scripting. I'm using the cygwin shell to control custom c-drivers running in windows for data-acquisition modules...Anyways, so I tried the solution suggested by @elixir_sinari and it worked. I don't understand why it works, i.e. why the single quotes and why I need to use eval. More broadly, though, I'm trying to understand what @alister is suggesting. This var2 variable is actually part of a script file that I'm developing, it contains the path to my c-driver executable. I'm utilizing this path in a lot of places in my code. Since I may change this location later, I wanted to store it in a variable that can easily be modified later. What is the alternative to this?

Now, further along in my scripting, I have another issue and it is as follows:
I have 3 variable declared
Code:
handle=2
digPath="/'cygdrive/c/2i/test fixture/software/mccdaqtest/debug/Dig'"

if I do
Code:
var2="$digPath $handle"
echo $var2

I get
Code:
/'cygdrive/c/2i/test fixture/software/mccdaqtest/debug/Dig' 2

so far, so good.
but if I do:
Code:
var2="$digPath $handle config"
echo $var2

then I get this:
Code:
configive/c/2i/test fixture/software/mccdaqtest/debug/Dig' 0

What the dicken's is going on here? Why is my line being overwritten by config
. At first I thought there was a limit to the number of characters per line in bash, but I did google on and found getconf ARG_MAX which returns 32000, so that isn't the issue.
# 7  
Old 04-23-2013
Quote:
Originally Posted by oahmad
What the dicken's is going on here? Why is my line being overwritten by config
Almost certainly, you've been editing scripts with Microsoft Notepad and ended up with carriage returns inside them.

Code:
tr -d '\r' < wingarbage > unixtext

Now, back to the subject at hand, you can't embed quotes inside quotes, they will not be evaluated later unless you shoehorn with eval.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Bash] passing variables to executable doesn't work

Bash version 4.4.20 / Ubuntu 16.0.4 Hello, I tried to write a script that gathers some data and passes them to an executable. The executed application answers with an error. The echo output in the script returns correct values. If I copy/paste the last echo command, it get's executed... (2 Replies)
Discussion started by: sushi2k7
2 Replies

2. Shell Programming and Scripting

Bash script make itself executable

Is there a way to make this make itself executable? Thanks. :-) cat > somescript.sh << \EOF #!/bin/bash block_count=$(sudo tune2fs -l /dev/sda1 | awk '/^Block count:/ {print $NF}') reserved_block_count=$(sudo tune2fs -l /dev/sda1 | awk '/^Reserved block count:/ {print $NF}') perl -e... (4 Replies)
Discussion started by: drew77
4 Replies

3. Shell Programming and Scripting

Making bash script allways executable when transfer ?

Does it possible to make some bash script automatic to be a executable when transfered to another pc...? (5 Replies)
Discussion started by: tomislav91
5 Replies

4. Shell Programming and Scripting

How to create an executable bash script for these commands?

I wish to create an executable bash script that will run the following commands as root, that is, using sudo su iptables-save | awk '/^ / { print $1 } /^:+ / { print $1 " ACCEPT" ; } /COMMIT/ { print $0; }' | iptables-restoreMy first attempt at bash... (9 Replies)
Discussion started by: thixeqi
9 Replies

5. UNIX for Dummies Questions & Answers

Problem running executable with ./

Hey all, I'm trying to execute a program and despite it appearing to be there, I keep getting this: -bash: ./aisdispatcher: No such file or directoryTo run it, I'm going into the directory where it is stored and running ./aisdispatcher...the result of which should just be a listing of options... (10 Replies)
Discussion started by: pmd006
10 Replies

6. UNIX for Dummies Questions & Answers

Running Executable in Bash Script

Hey guys, so I've been trying to write a bash script called runSorter.sh that runs an executable that also takes in some parameters and outputs the results to a text file. The executable, sorter, takes in a number parameter. I want to make it so that you can input as many number parameters into... (4 Replies)
Discussion started by: Duo11
4 Replies

7. UNIX for Advanced & Expert Users

How to stop direct running of executable

Dear Sir, I am using CentOS-5.2(64-bit) as an server side OS in a cluster with 32 slaves+1 Master. My question is, after compiling a file with ifort, I am suppose to get a executable(say a.out). I want my users to do ssh slave.local and then do ./a.out But is it possible to restrict... (0 Replies)
Discussion started by: snbanerjee
0 Replies

8. UNIX for Dummies Questions & Answers

running command prompt executable file in shell script

hi i have file extentioned with test.vbs. i am able to run this file n execute through command promt but i dont know how to run in shell script example: file name is test.vbs which contains strSoundFile = "C:\windows\Media\Notify.wav" Set objShell = CreateObject("Wscript.Shell") strCommand... (5 Replies)
Discussion started by: atl@mav
5 Replies

9. Shell Programming and Scripting

Invoking CGI executable after setenv (in bash)

Hello, I have an executable cgi program that I can run manually from my Linux shell after setting environmental variables on the previous line, like this: setenv QUERY_STRING "workdir=/u/here/there/&nb1=5&nb2=1000" MyExecutable.cgiHow can I imitate this behavior in a bash script? I tried... (11 Replies)
Discussion started by: aplaydoc
11 Replies

10. Programming

Running an executable file

I've created a c program and compiled it with gcc, in unix. The file name is abc.c and it is run by typing the command ./abc I have another program which creates a child process, and I need this abc program to run on that child process. I've tried execvp(), but it doesn't work. How can I run... (2 Replies)
Discussion started by: sdsd
2 Replies
Login or Register to Ask a Question