7

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.

1
  • stupid, stupid me. inserting one line into the other did the trick: sed -n "$(printf "%02d" $(echo "$i+1"|bc))q;d" /var/www/playlisten.txt|cut -c 4- sorry for bothering any of you... ;) Commented Jun 29, 2011 at 10:38

3 Answers 3

12

You need to use ${c}q to prevent the greedy treatment (bash trying to use as many valid characters as possible):

pax$ export cq=111

pax$ export c=222

pax$ echo $cq
111

pax$ echo ${c}q
222q

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 get bash to 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 sed an entire file, you're better off doing that in a dedicated external tool but bash provides quite a bit of power to replace expensive operations like i=$(expr $i + 1) or i=$(echo "$i+1"|bc).

A bash loop can be done thus with the increment and other calculations being handled without external processes:

#!/bin/bash
for ((count = 0; count < 10; count++)) ; do
    ((next = count + 1))
    echo "count is ${count}, sed string is '${next}q;d'."
done

This outputs:

count is 0, sed string is '1q;d'.
count is 1, sed string is '2q;d'.
count is 2, sed string is '3q;d'.
count is 3, sed string is '4q;d'.
count is 4, sed string is '5q;d'.
count is 5, sed string is '6q;d'.
count is 6, sed string is '7q;d'.
count is 7, sed string is '8q;d'.
count is 8, sed string is '9q;d'.
count is 9, sed string is '10q;d'.

You could also incorporate next into the for loop as well:

#!/bin/bash
for ((count = 0, next = 1; count < 10; count++, next++)) ; do
    echo "count is ${count}, sed string is '${next}q;d'."
done
Sign up to request clarification or add additional context in comments.

Comments

2

Put the variable name in braces:

c=$(printf "%02d" $(echo "$i+1"|bc))
sed -n "${c}q;d" /var/www/playlisten.txt|cut -c 4-

Comments

2

bit shorter

sed -n "$(printf "%02d" $((i+1)))q;d" /var/www/playlisten.txt|cut -c 4-

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.