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

Post here if you are a new Porteus member and you're looking for some help.
User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

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

Post#1 by Ed_P » 10 Sep 2014, 20:05

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:~$ 
Last edited by Ed_P on 11 Sep 2014, 04:50, edited 3 times in total.
Ed

sean
Contributor
Contributor
Posts: 166
Joined: 08 Jul 2012, 02:30
Distribution: Porteus v3.0 LXDE i486
Location: South Central PA, USA

Re: Why can't I see $1?

Post#2 by sean » 10 Sep 2014, 20:31

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

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Re: Why can't I see $1?

Post#3 by Ed_P » 10 Sep 2014, 21:32

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:
Ed

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Re: Why can't I see $1?

Post#4 by Ed_P » 10 Sep 2014, 22:07

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:
Ed

sean
Contributor
Contributor
Posts: 166
Joined: 08 Jul 2012, 02:30
Distribution: Porteus v3.0 LXDE i486
Location: South Central PA, USA

Re: Why can't I see $1?

Post#5 by sean » 11 Sep 2014, 00:58

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

User avatar
brokenman
Site Admin
Site Admin
Posts: 6105
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v4 all desktops
Location: Brazil

Re: Why can't I see $1?

Post#6 by brokenman » 11 Sep 2014, 02:43

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"
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Re: Why can't I see $1?

Post#7 by Ed_P » 11 Sep 2014, 04:39

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.
Ed

User avatar
brokenman
Site Admin
Site Admin
Posts: 6105
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v4 all desktops
Location: Brazil

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

Post#8 by brokenman » 11 Sep 2014, 13:50

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
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

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

Post#9 by Ed_P » 11 Sep 2014, 17:46

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.
Ed

Post Reply