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

10 answers

  1. Moderator
    Hello,

    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. 
    8
    1. 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
      1
      1. 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
        0
      2. 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
        0
    2. 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
      1
      1. Moderator
        In Bourne shell, it is not possible to initialize a variable in the command "export"; it must be done in two steps:
        variable=valeur export variable
        ;-)
        --
        Z'@+...che.
        JP : Zen, my Nuggets ! ;-) Knowledge is only valuable when it is shared.
        0
    3. 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
      0
      1. Moderator
        I 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. 
        0
        1. 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.
          0
          1. Is it the same thing as sh or tcsh?

            Thank you
            0
            1. 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 
              0
              1. 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?
                -1