Bash assign string to variable

Hi ,I am trying to assign string to variable ,but it doesn't work
Also could you show me different ways to use grep,(I am trying to get the first,second and first column form file,and I am counting the chars)

let name=`grep "$id" product | cut -c6-20`


What is it that you're trying to achieve here? Can you give an example?
You want to search something from the first 2 lines? And also the first column entirely?

You would be better off using awk and sed.

I am extracting the second column in the file product ,and trying to assign this column which is string to variable name
Thanks

Ok. So if an input file looks like this:

Column01 Column02 Column03
Column11 Column12 Column13
Column21 Column22 Column23

Then you have 2 ways of doing this:

  1. Do it by reading the file line by line:
while read FILE_LINE
do
	MULTIPLE_COMMANDS=`echo $FILE_LINE | awk '{print $2}'`
	echo $MULTIPLE_COMMANDS
done < $INPUT_FILE

Output:
Column02
Column12
Column22
  1. Do it by using just one command:
SINGLE_COMMAND=`cat $1 | awk '{print $2}'`
echo $SINGLE_COMMAND

Output:
Column02 Column12 Column22

let is an arithmetic assignment in ksh. Just leave it out:

name=`grep "$id" product | cut -c6-20`

or

name=$(grep "$id" product | cut -c6-20)

Thanks ,it works
How can I check if the product id exist with this line
`grep "$id" invertory | cut -c6-10`
what kind of return is going to have if it is eof and it didnt find it

#!/bin/bash
echo -n "Please provide a product id: "
read id
let qty=`grep "$id" invertory | cut -c6-10`
echo $qty
let price=`grep "$id" invertory | cut -c11-15`
let result=$qty\*$price
name=$(grep "$id" product | cut -c6-20)    
echo $name  total is $result
 

as well as bash. it's actually all right to use it.

---------- Post updated at 08:39 PM ---------- Previous update was at 08:35 PM ----------

man test. you can find test options to check for null strings. Or you can try checking $?

Thanks ,I changed the code but now var price doesnt have return result
Any idea why

 
#!/bin/bash
echo -n "Please provide a product id: "
read id
line=`grep "$id" inventory`
if(($(echo ${#line})==0))
then
{
echo No mach
}
else
{
qty=`echo $line | cut -c6-10`
price=`echo $line | cut -c11-15`    
echo price $price
echo quantity $qty
}
fi

 

don't use braces with your if/else. ( braces {} have different meaning in shell ). you can use -n or -z in your if/else to check for string size

Thanks ,could you show me how to use it with code

The braces are unnecessary, but don't affect anything.

In bash (and ksh93), you can use the substring expansion instead of the external command, cut:

if [ -z "$line" ]
then
  echo No match
else
  qty=${line:5:5}
  price=${line:6:5}
  echo price $price
  echo quantity $qty
fi

OK, but it it should not be regarded as just another way to assign something to a variable:

$ let x=a
$ echo $x
0
$ x=a
$ echo $x
a

sure, because after all let is used for arithmetic, since its what OP is doing anyway storing prices, quantities etc. It is still all right to use it in his case.

There is no good reason for using let. It is not standard, and it adds nothing.

whether there is good reason or not, technically speaking, its still a valid way to do maths. Some may like do things this way

$ m=0
$ let "m=m+1" # or let "m+=1"
$ echo $m
1

and some may like it this way too

$((m++))

In the end, its just a matter of choice. Isn't it?

Speaking of not standard, a lot of things pertaining to different shells are not standard either.

That's not portable (i.e., POSIX); it is not a valid Unix shell statement.
It may work in some shells, but there's no guarantee.

The reliable way is:

$(( m += 1 ))

No. There is no good reason to write unportable syntax when the portable is just as efficient.

True, and they should only be used when the portable method is less efficient.

The vast majority of scripts can be written using only POSIX syntax.

that's my illustration of being not standard, but yet is also a valid syntax provided by bash.

don't work in bourne shell

not talking about portable or not portable here. Bash provides the facility to write non portable code such as the syntax I described, but are you going to stop every user who bash not to write it that way?

efficient? who cares (except cfajohnson)? are you going to next show me proof that let "m+=1" is slower or faster the $((m+1)) ?

If there are only one type of shell in this world, who needs a POSIX standard? while I do not disagree with you on this, not all scripters want to write in POSIX mode as well. A vast majority of them prefers to utilize their shell to the fullest to get the job done.

IOW, it is limited to bash. Why do that when you can write in a way that works in bash as well as all other POSIX shells?

Nor does any other form of arithmetic. But the Bourne shell is not a standard Unix shell (it is a precursor to it).

It is much faster when you have to rewrite your script for a standard shell that doesn't support that extension.

There are many shells; that's why the standard is important.

I, too, utilize the shell to the fullest. The way I do that is to use the most portable syntax available. When I need something that is not available in the standard shell, then and only then do I use the more parochial syntax.

if you are talking about using let, I personally would not use it, but i don't condone it either if its used appropriately. In this world, not all want to write in POSIX mode. Why? If I am using bash 4 and I want to change case for example, I want to use $^ or $, instead of calling external commands since they are internals and I don't have to use it in other shells... does that answer why?
take note, that we are OT already. My only comment is that "let" is technically correct and legal to be used in OP's context and not a question of writing POSIX or not.

You said the more reliable way is $((m+=1)). I said it does not work in bourne shell. But if you want to talk about POSIX and stuff and if I am still using the bourne shell or tcsh, then it will not work right? In any case, this is also OT

I know why the standard comes about. You did not get my point and I am too lazy to explain my point to you either.

so just to get this clear, you do write in a way that is usable in bourne shell, zsh , tcsh , dash, etc, correct?

Isn't that what I said? When I want to do something that is more efficiently done using a non-portable syntax, I do that. (BTW, you can thank me for suggesting the ${var^} and ${var,} expansions to Chet Ramey.)

However, if I am unable to use bash4 (it is not installed on some systems I use) I don't use external commands for those conversions; I use functions that use only the shell.

I write, as much as possible, in a way that will run in any standard Unix shell (that is, a POSIX shell).