My bash scripts has a variable $c which is called within a sed-line, directly followed by another parameter – and bash (actually fairly logical) seems to think that the other parameter belong to the variable name, rendering it useless/empty.
(...)
c=$(printf "%02d" $(echo "$i+1"|bc))
sed -n "$cq;d" /var/www/playlisten.txt|cut -c 4-
(...)
The first line sets a temp variable, then it is called as a sed argument. I need to show bash that $cends after the c and that the variable is not named $cq (which is empty, of course)…
Any ideas would be highly appreciated, as always.
Thanks,
Christian.
PS. What I’m trying to accomplish 🙂 with this is having a for-loop that steps thru 00..50, within the loop the number itself is needed but also the number +1. Just in case anyone want’s to know.
You need to use
${c}qto prevent the greedy treatment (bashtrying to use as many valid characters as possible):I should also mention that, if performance is important to you, you want to try and minimise how many external processes (like
bc) you run to do your task. Forking and exec’ing are not cost-free actions and you’ll run much faster if you getbashto do most of the work itself for short lived tasks.By short-lived I mean things like adding one to a variable. Obviously if you want to do a big job like
sedan entire file, you’re better off doing that in a dedicated external tool butbashprovides quite a bit of power to replace expensive operations likei=$(expr $i + 1)ori=$(echo "$i+1"|bc).A
bashloop can be done thus with the increment and other calculations being handled without external processes:This outputs:
You could also incorporate
nextinto theforloop as well: