LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2016, 06:48 PM   #1
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,971

Rep: Reputation: 271Reputation: 271Reputation: 271
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.
 
Old 09-20-2016, 07:23 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
Old 09-20-2016, 08:19 PM   #3
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,998

Rep: Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629
I like to use autoexpect to make scripts. They tend to work more often.
 
Old 09-21-2016, 04:24 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by RandomTroll View Post
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?
 
Old 09-21-2016, 04:27 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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.
 
Old 09-21-2016, 05:45 AM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
1 line of code is not a 'script'.
Show us the whole script and the cron please, or else it's guessing, at best.
 
Old 09-21-2016, 07:44 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Habitual View Post
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.
 
Old 09-21-2016, 12:59 PM   #8
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,971

Original Poster
Rep: Reputation: 271Reputation: 271Reputation: 271
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.
 
Old 09-21-2016, 01:16 PM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by RandomTroll View Post
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?

Last edited by Habitual; 09-21-2016 at 06:43 PM. Reason: 'in some distros' added. Lions, Tigers, and Bears, Oh my!
 
Old 09-21-2016, 02:49 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
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?
 
Old 09-21-2016, 06:13 PM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
/usr/local/bin/lynx path is suspect, should be /usr/bin/lynx in Slackware

$HOME is set in cron env for Slackware as well
 
Old 09-22-2016, 02:19 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
Quote:
Originally Posted by RandomTroll View Post
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?
 
1 members found this post helpful.
Old 09-22-2016, 06:32 PM   #13
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,971

Original Poster
Rep: Reputation: 271Reputation: 271Reputation: 271
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.
 
Old 09-22-2016, 06:45 PM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I overlooked the first line of the script:
Code:
#! /bin/bash
Is it really written as this?
 
Old 09-22-2016, 11:49 PM   #15
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
[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)

Last edited by Jjanel; 09-23-2016 at 03:14 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [SOLVED] bash script: can echo command, command works if I type, but command doesn't work in script ... why? hopeless_n00b Linux - Newbie 10 07-12-2018 05:57 AM
How command line options work after switching the user in script? visu_kvg Linux - General 7 02-21-2013 03:48 PM
mkdir won't work in a script but works on command line ifeatu Linux - Newbie 3 02-04-2010 02:26 PM
Perl shell-out to script dunna work. Works on command line. Why? jlangelier Linux - Software 1 08-28-2004 02:00 AM
script runs fine from a command line, but doesn't work from cron? kleptophobiac Linux - Software 5 05-03-2004 04:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration