2

I have the following line at the first line in my script file:

#!/bin/sh

So I'm using csh.(?)

I wanto assign the output of the following to an environment variable:

echo $MYUSR | awk '{print substr($0,4)}'

I try:

set $MYVAR = echo $MYUSR | awk '{print substr($0,4)}'

But it doesn't work, How can I do it? I want to do it in a sh file.

2
  • no, /bin/sh is bourne shell or sometimes replaced by bash Commented Mar 16, 2012 at 14:55
  • I'm curious, did you mean for your script to be sh or csh? Commented Mar 17, 2012 at 10:07

4 Answers 4

2

Your script should look like

 #!/bin/csh

 set MYVAR = `echo $MYUSR | awk '{print substr($0,4)}'`

 echo $MYVAR

I don't have a way to test this right now, let me now if it doesn't work.


If you've inherited the basis of your script from someone else, with the #!/bin/sh, then you have to find out if /bin/sh is really the bourne shell, or if it is a link to /bin/bash

You can tell that by doing

   ls -l /bin/sh /bin/bash

if you get back information on files where the size is exactly the same, the you're really using bash, but called as /bin/sh

So try these 2 solutions

   MYVAR=$(echo $MYUSR | awk '{print substr($0,4)}')
   echo $MYVAR

AND

   MYVAR=``echo $MYUSR | awk '{print substr($0,4)}``  
   echo $MYVAR

   # arg!! only one pair of enclosing back-ticks needed, 
   # can't find the secret escape codes to make this look exactly right.

in all cases (csh) included, the back-ticks AND the $( ... ) are known as command substitution. What every output comes from running the command inside, is substituted into the command line AND then the whole command is executed.

I hope this helps.

Sign up to request clarification or add additional context in comments.

9 Comments

I get this: sh-3.1$ ls -l /bin/sh /bin/bash -rwxr-xr-x 1 root root 785856 Jul 12 2006 /bin/bash lrwxrwxrwx 1 root root 4 Jun 15 2010 /bin/sh -> bash Do I use bash? should I export MYVAR in the end? Thanks
yes, your /bin/sh and /bin/bash are really the same program. In some systems, using /bin/sh limits the features available in /bin/bash to those in Bourne shell (/bin/sh on older Unix systems like Solaris/HP/AIX, et.al). If you are doing this for yourself, you definitely want to use bash going forward. If you're doing it for a job, you have to find out if it is OK to do so.
...... You only need to export variables that are required for use inside a sub-shell environment. You can either test if you program works without the export OR you can just go ahead and export it. That being said, I don't recommend exporting every variable, as that increases memory usage for no real good purpose.
I tried MYVAR = $(echo $MYUSR | awk '{print substr($0,21)}') export MYVAR but it doesnt display anythin when I do echo $MYVAR in the application xterm, what went wrong?
what is in your var $MYUSR ? ALSO no spaces allowed around '=', is that a typo or did you actually run with spaces around '='? AND, I'm assuming that you really had a export MYVAR on a separate line? If not, you should use MYVAR=$(echo $MYUSR | awk '{print substr($0,21)}') ; export MYVAR (note the semicolon). Good luck.
|
0

if it's /bin/sh it's bourne shell or bash, and use back quotes to execute something and this to assign that...

MYVAR=`echo $MYUSR | awk ...`

2 Comments

when I do echo $SHELL I get /bin/csh. When I do MYVAR = echo $CARMUSR | awk '{print substr($0,21)}'`` I get "command not found" error.
your command-line shell might be csh, but the script you're writing, if it has /bin/sh after the #!, will run in bourne shell or bash.
0

That script first line indicates that it should be interpreted by the Bourne shell (sh), not csh. Change it to

#!/bin/csh

2 Comments

Oops, the initial hash mark didn't make it through the html. The change is substitute csh for sh.
In the future, use 4 spaces in front of code samples, then the '#' looses its meaning (inside S.O.) to make the text bold. Also note at top of text input box, there are a series of editing tools, including {} to auto-indent (With 4 spaces) a block of text, so it will be presented as-is, i.e. for code samples, program output, etc. Good luck and keep posting.
0

The first line of your code shows clearly you are not using a csh. You are using a plain sh environment/shell. You have 2 options:

  1. Either change the first line to #!/bin/csh OR
  2. Keeping first line unchanged, update the code for setting the variable.

    MYVAR=`echo $MYUSR | awk '{print substr($0,4)}`
    echo $MYVAR
    

Let me know, if you get any error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.