Page 1 of 1

Why can't I see $1? [Solved]

Posted: 10 Sep 2014, 20:05
by Ed_P
I have tried with and without " marks around the variables but it doesn't display. I can sense it's presence but not it's value. What am I doing wrong?

Code: Select all

#!/bin/sh 

PRESSENTER="$1"
echo PRESSENTER=$PRESENTER
if [ "$PRESSENTER" = "" ]; then
  echo -en "Enter Y to continue. "
  read
fi
exit

Code: Select all

guest@porteus:~$ test.sh 
PRESSENTER=
Enter Y to continue. Y
guest@porteus:~$ test.sh Y
PRESSENTER=
guest@porteus:~$ 

Re: Why can't I see $1?

Posted: 10 Sep 2014, 20:31
by sean
Ed_P,

I know absolutely nothing about what you are doing, however, on the first "echo" line of the script, the second PRESENTER has only one "S".

That's about the extent of my knowledge :-)

Sean

Re: Why can't I see $1?

Posted: 10 Sep 2014, 21:32
by Ed_P
sean wrote:I know absolutely nothing about what you are doing, however, on the first "echo" line of the script, the second PRESENTER has only one "S".
:oops: Good eyes sean, good eyes. Thank you.
That's about the extent of my knowledge :-)
And that gives you an idea the extent of mine.

Thanks again. :beer:

Re: Why can't I see $1?

Posted: 10 Sep 2014, 22:07
by Ed_P
OK, a slightly new wrinkle.

I modified the script to run in root mode.

Code: Select all

PRESS_ENTER="$1"
echo PRESS_ENTER=$PRESS_ENTER
if [ `whoami` != "root" ]; then
  su -c "sh $0"
  exit
fi
echo PRESS_ENTER=$PRESS_ENTER
if [ "$PRESS_ENTER" = "" ]; then
  echo -en "Enter Y to continue. "
  read
fi

exit
And the results are more than a little unexpected!! At least for me.

Code: Select all

guest@porteus:~$ test.sh y
PRESS_ENTER=y
Password: 
PRESS_ENTER=
PRESS_ENTER=
Enter Y to continue. 
guest@porteus:~$ 
The script starts over and looses the $1 value!!


=update=

hmmmm The save-session script seems to provide a clue. :wink:

Re: Why can't I see $1?

Posted: 11 Sep 2014, 00:58
by sean
Ed_P,

As I mentioned, I have extremely little knowledge of scripts .............. or ........... most anything:-)

But looking again at your original post the top line reads #!/bin/sh and I'm not familiar with that heading for a bash script. Perhaps you are using something else, but my understanding is that a bash script starts with:

#!/bin/bash

Pardon me if I'm really showing how much I don't know, just another thought.

Mind if I ask what it is you are trying to accomplish with this?

Sean

Re: Why can't I see $1?

Posted: 11 Sep 2014, 02:43
by brokenman
Keen eye sean. It wold matter in some other distros too, but in porteus sh is really a symlink to bash. So it won't matter if the 'shebang' is #!/bin/bash or #!/bin/sh ... it is all thrown to bash. The correct form should be #!/bin/bash in porteus but really only so anyone reading the code understands that it is written for a bash shell (the two differ slightly)

Code: Select all

guest@porteus:~$ file $(which sh)
/bin/sh: symbolic link to `bash'
Your script runs with your first argument 'y' until it reaches your root check. Then you enter your password and it reruns itself as you asked it, without any arguments.
I think this is the same problem you had with a previous script. Don't forget to rerun your script with arguments.

su -c "sh $0"

Try: su -c "sh $0 $1"

Re: Why can't I see $1?

Posted: 11 Sep 2014, 04:39
by Ed_P
brokenman wrote:Keen eye sean.
He does have that, for sure.
I think this is the same problem you had with a previous script. Don't forget to rerun your script with arguments.

su -c "sh $0"

Try: su -c "sh $0 $1"

A great thought, and excellent suggestion. Thank you Image brokenman, that worked.

I'd been trying to use the approach you used in the save-session (lines 26-35) script, but was not getting that quite right. My variable was getting assigned the file's name rather than it's contents. What was I doing wrong?

Code: Select all

#!/bin/bash 

if [ ! "$1" = "" ]; then
  echo $1 > /tmp/pe.tmp
fi

if [ `whoami` != "root" ]; then
  su -c "sh $0 $1"
  exit
fi

echo PRESS_ENTER=$PRESS_ENTER

PRESS_ENTER=""
if [ -a /tmp/.pe.tmp ]; then
  PRESS_ENTER="</tmp/pe.tmp"
  rm            /tmp/pe.tmp
fi

echo PRESS_ENTER=$PRESS_ENTER
if [ "$PRESS_ENTER" = "" ]; then
  echo -en "Enter Y to continue. "
  read 
fi
exit

Code: Select all

guest@porteus:~$ test.sh y 
Password: 
PRESS_ENTER=
PRESS_ENTER=</tmp/pe.tmp
guest@porteus:~$ 
sean wrote:Mind if I ask what it is you are trying to accomplish with this?
Not at all. The code you see is a test piece for another script that I want to modify to run as a desktop app. This other script does two functions which I manually control when I run it from terminal mode. When I run it from a desktop icon I want it to do both functions without my manual input.

Re: Why can't I see $1? [Solved]

Posted: 11 Sep 2014, 13:50
by brokenman

Code: Select all

guest@porteus:~$ echo myVariable > test.txt
guest@porteus:~$ x=$(<test.txt)
guest@porteus:~$ echo $x
myVariable
Quotation marks is the problem. Exchange them for backticks `
In this example I have used $( to avoid any confusion about quotations but you can substitute them with backticks if you prefer.

Code: Select all

PRESS_ENTER=$(</tmp/pe.tmp)
Be aware that the more portable version would be:
PRESS_ENTER=`cat /tmp/pe.tmp`

There is really no need to dump this argument to a file unless another script will read it.

Code: Select all

#!/bin/bash 

## If a first argument exists then make PRESS_ENTER equal to it.
[ $1 ] && PRESS_ENTER=$1

if [ `whoami` != "root" ]; then
  su -c "sh $0 $1"
  exit
fi

echo PRESS_ENTER=$PRESS_ENTER

# Check if $PRESS_ENTER is empty
if [ -z "$PRESS_ENTER" ]; then
  echo -en "Enter Y to continue. "
  read 
fi
exit

Re: Why can't I see $1? [Solved]

Posted: 11 Sep 2014, 17:46
by Ed_P
brokenman wrote: Quotation marks is the problem. Exchange them for backticks `
In this example I have used $( to avoid any confusion about quotations but you can substitute them with backticks if you prefer.
Be aware that the more portable version would be:
PRESS_ENTER=`cat /tmp/pe.tmp`
There is really no need to dump this argument to a file unless another script will read it.

Code: Select all

#!/bin/bash 

## If a first argument exists then make PRESS_ENTER equal to it.
[ $1 ] && PRESS_ENTER=$1

if [ `whoami` != "root" ]; then
  su -c "sh $0 $1"
  exit
fi

echo PRESS_ENTER=$PRESS_ENTER

# Check if $PRESS_ENTER is empty
if [ -z "$PRESS_ENTER" ]; then
  echo -en "Enter Y to continue. "
  read 
fi
exit
Wow!! Excellent suggestions all. Thank you Image Master.

There is so, so, so much to learn.