Help executing command with options

Hi,

I have this command in a shell script and I can get it to echo ok, but when I try to execute the command I get a "file not found" error. Which is strange because, if I copy and paste the same command at the cli it works ok.

What am I doing wrong please?

It's probably failing to fine ./executable could the working directory be different in the script? I'd suggest putting a full path to the "executable" file.

Tried the full path, but that's not working either.

to debug shell script, you can try this (you can replace with ksh or bash for your real environment)

sh -x ./executable

So I treated the command as I would a line with spaces,

Now I'm getting "0403-005 Cannot create the specified file".

wth?

You seem to have some issues with quoting here, what does the final quote on your command match?

>\ /send

This is trying to create output under folder " /send", i.e. a directory name "send" under a directory with a single space as its name (" ") in the current directory.

There is no quote (typo on my part).

The output directory is a log directory that doesn't have a space.

Here's the command.

Do you really need to assign O1 O2 and O3 on the command line?

Why not simplify the command and assign them first:

O1='$input'
O2='data2'
O3='data3'
./executable > /send/output/to/log`date`.txt

How will I read the options (O1,O2,O3) to the command if I do it this way?

---------- Post updated at 11:39 PM ---------- Previous update was at 10:42 PM ----------

Ah, I see what you're saying. The problem is "O1 O2 O3" are literal stings that must go with the command. Let me play around with this a little to see if I can come up with something.

try using the export command:

export O1 O2 O3

Like this?

No either:

O1='$input'
O2='data2'
O3='data3'
 
export O1 O2 O3 
./executable > /send/output/to/log`date`.txt 
 

or

O1='$input'
O2='data2'
O3='data3'
 
export O1 O2 O3 ; ./executable > /send/output/to/log`date`.txt 

Nah. That doesn't appear to be working. Although all the errors have gone away, it's not running the ./executable or logging to the log directory. The end command has to be "O1 O2 O3 ./executable ..."

---------- Post updated at 11:13 AM ---------- Previous update was at 12:22 AM ----------

I've tried a few options but I can't get this to work.

Should I be using a variable with the export?

You are putting in extra quotes in front of it that foul it up.

O1="$input" O2='data2' O3='data3' ./executable > "/send/output/to/log`date`.txt"

I agree with you, however I can't figure out where I'm messing up. I need the quotes because the literal command string is so strange.

Here's the exact command I'm trying to do in the script.

The command has to be that way (with the single quotes and everything).

Because the O1 value will be the only value that will change, I need the extra quotes to ensure the O1 value gets input exactly as O1='input value'.

The problem I think I'm having is the environment is looking at 'O1=' as something it should be substituting, however, there is no associated value. I need to be able to call the exact sting as one literal string and execute that.

Please at least try exactly as I showed you -- word for word, letter for letter, keystroke for keystroke -- before telling me it's wrong:

O1="$input" O2='data2' O3='data3' ./executable > "/send/output/to/log`date`.txt"

Because frankly you've got some fundamental misunderstandings about how quotes work in shell.

You don't "need the extra quotes". They're what's stopping it from working. Have a look at what quotes do:

$ "cat /etc/passwd"

sh: cat /etc/passwd: not found

$ cat /etc/passwd

root:x:0:0:root:/home/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
...

$ # Things inside single quotes do not substitute.  echo '$VAR' prints $VAR, not asdf.
$ VAR=asdf
$ echo '$VAR'

$VAR

$ echo "$VAR"

asdf

$ Extra quotes to 'protect' it stop it from working
$ "A=B C=D E=F" echo asdf
sh: A=B C=D E=F: not found

$ # It's supposed to work like this
$ A=B C=D E=F echo asdf

asdf

$ # You can quote the individual parts as you please, but not the whole thing:
$ A="B" C="D" E='F' echo asdf

asdf

$

Putting the whole expression inside quotes will prevent it from splitting, causing the shell to take it as some strange filename and complain about no such file being found.

The quotes I used are exactly what is necessary. The quotes you replaced it with break it, because variables do not expand inside single quotes.

You're were right. I removed all the quotes including the quote around the $input and it worked. Thanks a mil for the help and the lesson.

1 Like