Set command
Hello,
I used the set command in a script, the part of the script is as follows:
#! /bin/sh
#
if [ -f /home/prj/itsec/.prjrc ]; then
echo " ftlog ftflag local-user site file"
else
echo "not found"
fi
set PRJHOME = `echo $PWD | cut -d / -f 4-5`
#set PRJHOME = `echo $PWD | awk -F / '{ print "/"$4"/"$5 }'`
echo $PRJHOME
#x = `echo "/"$PRJHOME1`
#echo $x
set PRJNAME = `grep "NAME" /home/gmira/*/.prjrc | cut -f2 -d=`
echo $PRJNAME
But when I execute the script, I get nothing in the echo
Could someone help me
Thank you
I used the set command in a script, the part of the script is as follows:
#! /bin/sh
#
if [ -f /home/prj/itsec/.prjrc ]; then
echo " ftlog ftflag local-user site file"
else
echo "not found"
fi
set PRJHOME = `echo $PWD | cut -d / -f 4-5`
#set PRJHOME = `echo $PWD | awk -F / '{ print "/"$4"/"$5 }'`
echo $PRJHOME
#x = `echo "/"$PRJHOME1`
#echo $x
set PRJNAME = `grep "NAME" /home/gmira/*/.prjrc | cut -f2 -d=`
echo $PRJNAME
But when I execute the script, I get nothing in the echo
Could someone help me
Thank you
10 answers
-
ModeratorHello,
The "set" command in the "sh" shell at least is used to set parameters and not to initialize parameters:[jp@Mandrake tmpfs]$ set moi = bonsoir [jp@Mandrake tmpfs]$ echo $* moi = bonsoir [jp@Mandrake tmpfs]$ echo $2 = [jp@Mandrake tmpfs]$ echo $3 bonsoir [jp@Mandrake tmpfs]$
Moreover, to initialize a variable, there should be no space between the variable name, the equal sign, and the variable value:[jp@Mandrake tmpfs]$ set moi = bonsoir [jp@Mandrake tmpfs]$ echo $moi [jp@Mandrake tmpfs]$ moi=bonsoir [jp@Mandrake tmpfs]$ echo $moi bonsoir [jp@Mandrake tmpfs]$
To initialize your variables, simply give their name followed by the equal sign and their value without spaces, and optionally in quotes if the value contains spaces or special characters:VAR="This is a variable"
--
Z'@+...che.JP : Zen, my Nuggets ! ;-) Knowledge is only good if it is shared.
-
Make a variable "permanent": add the variable in .bashrc ?!
For this piece of code that is stubborn... you MUST REMOVE the set:
#! /bin/sh # Comment that explains what we are doing if [ -f /home/prj/itsec/.prjrc ]; then echo " ftlog ftflag local-user site file" else echo "not found" fi # Comment that explains what we are doing PRJHOME="'echo $PWD | cut -d / -f 4-5'" echo $PRJHOME # Comment that explains what we are doing PRJNAME='grep "NAME" /home/.prjrc | cut -f2 -d=' echo $PRJNAME
-
Is it the same thing as sh or tcsh?
Thank you -
Ce n'est pas la même chose, bien que `sh` et `tcsh` soient tous deux des interpréteurs de commandes. `sh` est le shell Bourne, tandis que `tcsh` est une version améliorée de `csh`, qui offre des fonctionnalités supplémentaires comme l'édition de ligne de commande et l'auto-complétion.
-
I used export but it didn't work,
I did:
export name=value
but it didn’t work, it gives me the following error:
name=value: is not an identifier
I don't know how I should fix this problem -
but I want the variable $moi to be known not only inside the script but also elsewhere (exactly like an environment variable: $path, $user, ........)
that's why I used set-
so you can definitely forget about it
a variable defined in a subshell is never known by the parent shell, there is no way
if you want it to be known in the parent shell, instead of executing your file you need to source it in the parent shell
that is if your script is called torgnole you do
. torgnole
instead of
./torgnole or torgnole
--
blah blah blah -
I would like to add that not only will it need to be sourced but also, it will need to use export
Moreover, the variable will be defined ONLY in the shell where you source the file
Let's say you open two shells (in two virtual terminals) and you source the file in one of them, the variable will not be defined in the other anyway
--
gnagnagna
-
-
ModeratorI told you the set command is used to set parameters. Get rid of it!!!
[jp@Mandrake tmpfs]$ cat script.sh #! /bin/sh moi=good evening echo $moi [jp@Mandrake tmpfs]$ sh script.sh good evening [jp@Mandrake tmpfs]$
;-))
--
Z'@+...che.JP : Zen, my Nuggets ! ;-) Knowledge is only good when shared.
-
I even tried with this little example:
#! /bin/sh
set moi=good evening
echo $moi
but it doesn't work
do you know where the error comes from?
Thanks in advance -
Hello,
with the commands typed in the prompt like you showed me, it works, but in a file like the following:
#! /bin/sh
#
if [ -f /home/prj/itsec/.prjrc ]; then
echo " ftlog ftflag local-user site file"
else
echo "not found"
fi
set PRJHOME="`echo $PWD | cut -d / -f 4-5`"
echo $PRJHOME
set PRJNAME=`grep "NAME" /home/.prjrc | cut -f2 -d=`
echo $PRJNAME
it doesn't work!!!!!!
I don't know why? -
Hi,
still no response? :(