LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Command working on command line but not in shell script (https://www.linuxquestions.org/questions/linux-newbie-8/command-working-on-command-line-but-not-in-shell-script-4175594501/)

markplus 11-30-2016 07:29 AM

Command working on command line but not in shell script
 
Hello,

on my Raspberry Pi I've installed tightvnc server. in order to start the server, I've gotta write on the command line:
Quote:

vncserver :1
When using the command line ("vncserver :1"), it works flawlessly and results on following response:
PHP Code:

pi@raspberrypi:/var/www/shellscr vncserver :1

New 'X' desktop is raspberrypi:1

Starting applications specified in 
/home/pi/.vnc/xstartup
Log file is 
/home/pi/.vnc/raspberrypi:1.log 

When executing from within a shell script ("./script.sh"), the server won't start and the response is:
PHP Code:

pi@raspberrypi:/var/www/shellscr $ ./vnc.sh
TightVNC Server version 1.3.9

Usage
vncserver [<OPTIONS>] [:<DISPLAY#>]
       
vncserver -kill :<DISPLAY#>

<OPTIONSare Xtightvnc options, or:

        -
name <DESKTOP-NAME>
        -
depth <DEPTH>
        -
geometry <WIDTH>x<HEIGHT>
        -
httpport number
        
-basehttpport number
        
-alwaysshared
        
-nevershared
        
-pixelformat rgb<NNN>
        -
pixelformat bgr<NNN>

See vncserver and Xtightvnc manual pages for more information

It's apparently not a rights issue as it is executed by the same user and other shell scripts with the same structure work flawlessly.

Can anyone help?

jpollard 11-30-2016 07:42 AM

How are you logging in?
What is in the shell script?

markplus 11-30-2016 10:04 AM

Quote:

Originally Posted by jpollard (Post 5636205)
How are you logging in?
What is in the shell script?

I'm logging in via ssh. the shell script is:
PHP Code:

vncserver :


Shadow_7 11-30-2016 10:06 AM

Perhaps:

Code:

/usr/bin/vncserver -- ':1' &
in the script. The $PATH, id, or group might differ in script than on the cli. Some stuff you could add to help debug things.

Code:

echo "PATH:"
echo $PATH
echo "SHELL:"
echo $0
echo "ID:"
id
echo "which vncserver"
which vncserver

To debug differences between your shell execution and your CLI.

markplus 11-30-2016 11:09 AM

Quote:

Originally Posted by Shadow_7 (Post 5636280)
Perhaps:

Code:

/usr/bin/vncserver -- ':1' &
in the script. The $PATH, id, or group might differ in script than on the cli. Some stuff you could add to help debug things.

Code:

echo "PATH:"
echo $PATH
echo "SHELL:"
echo $0
echo "ID:"
id
echo "which vncserver"
which vncserver

To debug differences between your shell execution and your CLI.


Unfortunately the suggested command results in:
PHP Code:

pi@raspberrypi:/var/www/shellscr $ ./vnc.sh
./vnc.shline 1: $'\r'command not found 

The debugging script returns:
PHP Code:

pi@raspberrypi:/var/www/shellscr $ ./test.sh
PATH
:
/
usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
SHELL
:
./
test.sh
ID
:
uid=1000(pigid=1000(pigroups=1000(pi),4(adm),20(dialout),24(cdrom),27(sudo),29(audio),33(www-data),44(video),46(plugdev),60(games),100(users),101(input),108(netdev),997(gpio),998(i2c),999(spi)
which vncserver
/usr/bin/vncserver 


af7567 11-30-2016 01:54 PM

Quote:

Originally Posted by markplus (Post 5636279)
I'm logging in via ssh. the shell script is:
PHP Code:

vncserver :


What if your script was
Code:

#!/bin/sh

vncserver :1


c0wb0y 11-30-2016 02:03 PM

Quote:

Originally Posted by jpollard (Post 5636205)
How are you logging in?
What is in the shell script?

^ Care to post this?

szboardstretcher 11-30-2016 02:05 PM

That is answered in posts 3 and 5.

c0wb0y 11-30-2016 02:24 PM

Hmmm... I must be getting old or blind as I don't see any script contents.

szboardstretcher 11-30-2016 02:30 PM

A picture of his script then?

http://imgur.com/a/drNRw

Keeping in mind that shell scripts can be just lists of commands. Currently, his is a shell script with one line and one command.

Also, he is logging in with SSH as this user, as mentioned:

Quote:

uid=1000(pi) gid=1000(pi) groups=1000(pi)
Also - OP - please use this as the script - but TYPE IT IN and do NOT copy and paste it.

Code:

#!/bin/bash -x
/usr/bin/vncserver -- ':1' &


markplus 11-30-2016 02:39 PM

Quote:

Originally Posted by af7567 (Post 5636373)
What if your script was
Code:

#!/bin/sh

vncserver :1


This results in:
PHP Code:

pi@raspberrypi:/var/www/shellscr $ ./vnc.sh
-bash: ./vnc.sh: /bin/sh^Mbad interpreterNo such file or directory 

But /bin/sh exists.

szboardstretcher 11-30-2016 02:46 PM

As i said:

Please use this as the script - but TYPE IT IN and do NOT copy and paste it.

Code:

#!/bin/bash -x
/usr/bin/vncserver -- ':1' &

Every error you have pasted so far is due to copying and pasting. ^M and \r are both control characters incorrectly pasted from windows to nix.

markplus 11-30-2016 02:48 PM

Quote:

Originally Posted by szboardstretcher (Post 5636391)
A picture of his script then?

http://imgur.com/a/drNRw

Keeping in mind that shell scripts can be just lists of commands. Currently, his is a shell script with one line and one command.

Also, he is logging in with SSH as this user, as mentioned:



Also - OP - please use this as the script - but TYPE IT IN and do NOT copy and paste it.

Code:

#!/bin/bash -x
/usr/bin/vncserver -- ':1' &


Thank you also for explaining to c0wb0y!

The result of the manually typed code in the shell script, that you suggested unfortunately is:
PHP Code:

pi@raspberrypi:/var/www/shellscr $ ./vnc.sh
invalid option
Usage
:  /bin/bash [GNU long option] [option] ...
        /
bin/bash [GNU long option] [optionscript-file ...
GNU long options:
        --
debug
        
--debugger
        
--dump-po-strings
        
--dump-strings
        
--help
        
--init-file
        
--login
        
--noediting
        
--noprofile
        
--norc
        
--posix
        
--rcfile
        
--restricted
        
--verbose
        
--version
Shell options
:
        -
ilrsD or -c command or -O shopt_option         (invocation only)
        -
abefhkmnptuvxBCHP or -o option 


szboardstretcher 11-30-2016 02:49 PM

Just the second line then. Leave out the #!/bin/bash -x

Still type it though.

Code:

/usr/bin/vncserver -- ':1' &

c0wb0y 11-30-2016 02:53 PM

Let me grab my magnifying glass.

What's the content of:

Code:

$DISPLAY
$SHELL

and for good measure:
Code:

ls -l /bin/bash /bin/sh
printenv



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