1

I'm pretty new to linux, bash and so on. I have 2 problems(at the moment ...)both should be easy to solve.

All the code examples are .sh files and are called from the terminal with the sh file.sh command

First thing, why can i add the first string and not the second? it seems to me like the same operation...

DIR=$HOME/dir
DIR2=$DIR/test.php
echo $DIR
echo $DIR2

gives me this output:

/home/vm/dir
/test.phpdir

Second problem. I want to call the php-interpreter from an bash script.

#/home/vm/proj/test.php
<?php
echo __DIR__;

#home/vm/proj/script/script.sh
#! /bin/bash
PATH=$PATH:/$HOME/proj
php -f test.php

But this gives me always an error: couldnt find file...

So please help a noob and tell me hot to fix my code...

I can imageine that both of my problems are pretty simple to fix.

Thanks

EDIT:

System: Virtual Machine: Ubuntu 14.04.3 LTS, Trusty Tahr

Bash-version: 4.3.11(1)-release (x86_64-pc-linux-gnu)

M4tho
  • 116
  • 2
  • 13
  • What version of sh and what Linux distribution are you using? I cannot reproduce your problem, so it might be something specific. – Oldskool Aug 26 '15 at 11:13
  • `sh` and `bash` are not necessarily the same! Please read [here](http://stackoverflow.com/questions/5725296/difference-between-sh-and-bash). – FelixJN Aug 26 '15 at 11:15

1 Answers1

0

Try:

DIR2="$DIR/test.php"

You would execute PHP in CLI mode (even in a shell/bash script) like:

php /path/to/script.php

BTW I am using the same setup as you :)

EDIT:

#! /bin/bash
PATH=/path
PATH2="$PATH/to"
PATH3=$PATH2/script.php
echo $PATH3
# Output: /path/to/script.php

EDIT2:

Not sure why this is not working for you as it should.

See here for more information on concatenating variables in bash:

How to concatenate string variables in Bash?

See here for a possible work around for this issue: https://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables

Community
  • 1
  • 1
Craig van Tonder
  • 7,497
  • 18
  • 64
  • 109
  • Ok, thanks for the reply. Doublequotes dont work either. the problem with the path would matter later on when I try to use relative paths in includes, would it? – M4tho Aug 26 '15 at 11:26
  • Hmm, let me think about that then. I absolute paths in everything so i don't really have issues like this but then my environment does not change ;) – Craig van Tonder Aug 26 '15 at 11:28
  • @M4tho Something does not add up here with what you have said. See my update. – Craig van Tonder Aug 26 '15 at 11:33
  • I've copied your lines and it gives me "script.php" as output. It always overrides from the beginning of the string. no matter with method i use. ;( – M4tho Aug 26 '15 at 11:41
  • @M4tho what happens if you do something like `source test.sh`, does it give you any info? – Craig van Tonder Aug 26 '15 at 11:46
  • @M4tho the script is chmod 755 and owner is who? Admin/root user? – Craig van Tonder Aug 26 '15 at 11:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87987/discussion-between-m4tho-and-indigoidentity). – M4tho Aug 26 '15 at 11:55