LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why would a command work from the command line but not within a script? (https://www.linuxquestions.org/questions/programming-9/why-would-a-command-work-from-the-command-line-but-not-within-a-script-4175589827/)

RandomTroll 09-20-2016 06:48 PM

Why would a command work from the command line but not within a script?
 
I try to fetch Chappatte's comics from the 'New York Times'

For instance:

Code:

lynx -source https://static01.nyt.com/images/2016/09/20/opinion/20chappatte/20chappatte-facebookJumbo.jpg > Chappatte-20160920.jpeg
fetches September 20's. I wrote a script to automate this (generate the date) but it doesn't work. I made sure the script creates the correct command (by echoing it instead of running it).

The same happens with Heng's comics. I fetch dozens of other comics with a script.

keefaz 09-20-2016 07:23 PM

Did you set a crontab job? Maybe a path issue then
You should give a full path for Chappatte-20160920.jpeg, not sure if cron would save it in your home, /tmp or elsewhere

jefro 09-20-2016 08:19 PM

I like to use autoexpect to make scripts. They tend to work more often.

ondoho 09-21-2016 04:24 AM

Quote:

Originally Posted by RandomTroll (Post 5607967)
Code:

lynx -source https://static01.nyt.com/images/2016/09/20/opinion/20chappatte/20chappatte-facebookJumbo.jpg > Chappatte-20160920.jpeg

why isn't
Code:

wget https://static01.nyt.com/images/2016/09/20/opinion/20chappatte/20chappatte-facebookJumbo.jpg
sufficient?

and for the scripting, well, show us the script to see where it fails?

pan64 09-21-2016 04:27 AM

Quote:

Why would a command work from the command line but not within a script?
It usually depends on the environment (variables). So the environment of your command line shell and your script are not the same.

Habitual 09-21-2016 05:45 AM

1 line of code is not a 'script'.
Show us the whole script and the cron please, or else it's guessing, at best.

rtmistler 09-21-2016 07:44 AM

Quote:

Originally Posted by Habitual (Post 5608155)
1 line of code is not a 'script'.
Show us the whole script and the cron please, or else it's guessing, at best.

Second this and also noting that the question should be in a different forum. Have asked for it to be moved to either Newbie or Software.

RandomTroll 09-21-2016 12:59 PM

The whole script:

Code:

#! /bin/bash

/usr/local/bin/lynx -source https://static01.nyt.com/images/2016/09/20/opinion/20chappatte/20chappatte-facebookJumbo.jpg > $HOME/Cartoons/Chappatte/Chappatte-20160920.jpeg

All paths are correct.

Habitual 09-21-2016 01:16 PM

Quote:

Originally Posted by RandomTroll (Post 5608321)
The whole script:

Code:

#! /bin/bash

/usr/local/bin/lynx -source https://static01.nyt.com/images/2016/09/20/opinion/20chappatte/20chappatte-facebookJumbo.jpg > $HOME/Cartoons/Chappatte/Chappatte-20160920.jpeg

All paths are correct.

Cron doesn't understand $HOME in some distros # Edited Wed Sep 21 23:39:46 UTC 2016
Full path it out?

michaelk 09-21-2016 02:49 PM

The script works for me but since I changed the paths it may not be a true test. For the systems (debian and CentOS) that I am working with at the moment lynx is located in /usr/bin.

It depends on how cron's environment is configured. For debian $HOME is set.

Are you running this as a cron job or just from the command line? If it does not work from the command line are there any errors?

keefaz 09-21-2016 06:13 PM

/usr/local/bin/lynx path is suspect, should be /usr/bin/lynx in Slackware

$HOME is set in cron env for Slackware as well

pan64 09-22-2016 02:19 AM

Quote:

Originally Posted by RandomTroll (Post 5608321)
All paths are correct.

So everything is ok. No problem at all....


By the way can you tell us what's happened at all? Is there any error message somewhere? How did you start this script?

RandomTroll 09-22-2016 06:32 PM

crontab is irrelevant to this discussion.

I run the script from the command line.

I customize lynx so it's in /usr/local/bin. I told you it was correct.

It returns 'not found'.

The same happens with package lynx on a different computer.

I get around the problem by having the script also echo the correct command line so I can copy it with gpm then run it.

I didn't want to know another way to do it (wget works) but wanted to understand why there's a difference between running it straight and in a script.

snarf (which I usually use for things like this) also doesn't work.

keefaz 09-22-2016 06:45 PM

I overlooked the first line of the script:
Code:

#! /bin/bash
Is it really written as this?

Jjanel 09-22-2016 11:49 PM

[see 'Update' first...]
What's "not found"? (probably /usr/local/bin/lynx but it 'IS' there)

Try: sh -vx yourNYTscriptfilename
and/or put in, as 2nd line: (btw, what editor do you use? vi?)
set -vx

If you create [the 'SAME way'] a script with just a simple command like ls or df .
in it, does it work ok? [maybe; you mentioned other scripts work]
Try a script [*created the 'same way'*] with just: (omit 1st !# line [space was ok])

/usr/local/bin/lynx -version

Update:

I got it [MYscript, using /bin/ls in place of ...lynx] to say ONLY:
Quote:

/bin/ls: not found
by inserting a \r (ctrl-v ctrl-m in vi) in front of the command;
cat'ing the script looks 'ok', like post #8. (same for just ls)

Try: od -c yourNYTscriptfilename
and *carefully* look for \r or some other 'bad' character. (\n is 'good')

How [exactly] do you 'create' your 'nyt' scripts? (Microsoft 'does' \r)


All times are GMT -5. The time now is 01:17 AM.