Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I wrote following line in my script , but it not assigning any value to variable a

a= grep -n mark /etc/samba/smb.conf |cut -d: f1

I aspect line number where mark string present in smb.conf in variable a. But value of variable is null. What may be the issue

share|improve this question
add comment

5 Answers

up vote 10 down vote accepted

Two things:

  1. There can be no spaces between name of the variable, the = signal and the value of the variable
  2. If you want to put the results of a command or series of commands on a variable, you must enclose it on $(command) or old style backticks. Both lines below are correct and do the same thing:

    a=`commands`
    a=$(commands)
    

So your line must be

a=$(grep -n mark /etc/samba/smb.conf |cut -d: f1)

There are some differences on how to concatenate the use of backticks or new style $(), check this question on SO to see a good response.

share|improve this answer
    
+1. I recommend putting the delimiter character in single quotes, too: -d':'. It's not needed here, but it's a good habit to have. –  SmallClanger Mar 9 '11 at 13:06
add comment
a=`grep -n mark /etc/samba/smb.conf |cut -d: -f1`
share|improve this answer
add comment

Try this instead

a=$(grep -n mark /etc/samba/smb.conf |cut -d: f1)

Or instead of the $() you could use backticks (which here turn on the nice code look)

share|improve this answer
add comment

Should be a=$( ... ) or

a=` … `
share|improve this answer
add comment

Maybe by trying to use cut like this:

a=`grep -n mark /etc/samba/smb.conf |cut -d: -f1`
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.