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)
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?
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
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.
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).