I am trying to call a php script through a shell script.The script is to be called as the run on receive directive of gammu-smsd.My php script executes as expected when I call it using

/opt/lampp/bin/php myvarsreader.php

.However on trying to run my script(From terminal):

#!/bin/sh
PROGRAM=/opt/lampp/bin/php myvarsreader.php
    eval "$PROGRAM \"\${SMS_${i}_NUMBER}\" \"\${SMS_${i}_TEXT}\""
    echo $? 
    echo "$PROGRAM \"\${SMS_${i}_NUMBER}\" \"\${SMS_${i}_TEXT}\"" >> ran1date

I get the following error message

./first.sh: 17: myvarsreader.php: not found
eval: 1: : Permission denied
127

myvarsreader.php permissions are:-rwxrwxrwx,so I don't understand the permission denied part.
I am running ubuntu 10

Dani AI

Generated

Good progress so far — 's quoting tip fixed the "not found" error. The remaining problem (SMS values not arriving in your PHP) is usually an expansion/visibility issue, not PHP itself. Typical culprits: the index you use is unset, the shell running the hook is not Bash (Ubuntu’s /bin/sh is dash and lacks Bash indirect expansion), the hook runs under a different user/working directory (so relative paths fail), or the values are present only as environment variables rather than positional arguments. The most reliable approach is to invoke the PHP CLI with the concrete values as arguments so PHP receives them via $argv.

A compact, practical wrapper (requires Bash) that iterates message indices and calls PHP with explicit args:

#!/usr/bin/env bash
PHP_CLI="/opt/lampp/bin/php"
SCRIPT="/full/path/to/myvarsreader.php"

i=1
while :; do
  name="SMS_${i}_NUMBER"
  body="SMS_${i}_TEXT"
  number=${!name}     # Bash indirect expansion
  text=${!body}
  [ -z "$number" ] && break
  "$PHP_CLI" "$SCRIPT" "$number" "$text" >> /tmp/gammu_hook.log 2>&1
  i=$((i+1))
done

If you must keep /bin/sh, use guarded eval to fetch the dynamic names, but be careful with eval and untrusted input.

Inside PHP prefer to read positional args first and fall back to environment if needed:

<?php
$number = $argv[1] ?? getenv('SMS_1_NUMBER');
$text   = $argv[2] ?? getenv('SMS_1_TEXT');

Debug checklist: log the wrapper's environment (env > /tmp/gammu.env), whoami, and pwd; always use absolute paths for the PHP binary and the script; redirect stdout/stderr to a file so you can see PHP errors; confirm the hook runs as the expected user and that file permissions allow execution. Passing values as explicit CLI arguments avoids most shell-expansion pitfalls and is the cleanest solution.

I am trying to call a php script through a shell script.The script is to be called as the run on receive directive of gammu-smsd.My php script executes as expected when I call it using

/opt/lampp/bin/php myvarsreader.php

.However on trying to run my script(From terminal):

#!/bin/sh
PROGRAM=/opt/lampp/bin/php myvarsreader.php
    eval "$PROGRAM \"\${SMS_${i}_NUMBER}\" \"\${SMS_${i}_TEXT}\""
    echo $? 
    echo "$PROGRAM \"\${SMS_${i}_NUMBER}\" \"\${SMS_${i}_TEXT}\"" >> ran1date

I get the following error message

./first.sh: 17: myvarsreader.php: not found
eval: 1: : Permission denied
127

myvarsreader.php permissions are:-rwxrwxrwx,so I don't understand the permission denied part.
I am running ubuntu 10

Quote the command in assignment, i.e.

PROGRAM="/opt/lampp/bin/php myvarsreader.php"

Thanks ckoy,the php script is now called without any errors But no gammu-smsd environment variables are passed as arguments!?? Any suggestions?

If you want env vars to be accessible in a script, you must export them.
Another option is to pass them as arguments to the script but that requires some shell scripting knowledge.

OK so would you be kind enough to give a few ideas on how to go about exporting.By the way the .sh script is called by gammu-smsd does this change anything??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.