7
submitted 9 months ago* (last edited 9 months ago) by shortwavesurfer@monero.town to c/programming@beehaw.org

Any ideas? I am attempting to write a script that uses sed.

If done this way it fails

  • rmdec="sed 's/..................$//'"
  • i1xmr=$(echo "$i1p/$apiresponse*1000" |bc -l |$rmdec)

But if i do it this way it works

  • i1xmr=$(echo "$i1p/$apiresponse*1000" |bc -l | sed 's/..................$//')
all 22 comments
sorted by: hot top controversial new old
[-] I_Am_Jacks_____@lemmings.world 7 points 9 months ago

I have had some problems doing what you're trying to do. Replacing the variable with a function would work.

rmdec() { sed 's/...$//'; }

[-] shortwavesurfer@monero.town 1 points 9 months ago

Perfect. That did the trick. So when running a bash command like that putting it in a function keeps it from getting screwed up apparently

[-] Xyre@lemmus.org 5 points 9 months ago

You're assigning rmdec to the output of sed. It should work if you wrap it as you did with i1xmr.

[-] shortwavesurfer@monero.town 1 points 9 months ago

Yeah, my goal is to shorten that sed command to that variable. It seems like it would work, but nope. It throws errors

[-] Andy@programming.dev 1 points 9 months ago

It might be because it's a single string, and might work if you store it or expand it as an array. I think it would in Zsh, anyway.

But the response to use a function instead is probably wiser.

[-] brie@beehaw.org 3 points 9 months ago

Strings work fine, the problem is the (single) quotes:

~ $ foo="echo 'hello world'"
~ $ for x in $foo; do echo $x; done
echo
'hello
world'
~ $ $foo
'hello world'
~ $ eval "$foo"
hello world

The splitting is by whitespace, so the single quotes remain in the arguments. Using eval (and double quotes to preven splitting), it gets processed correctly. That said, don't use eval; use functions or aliases instead.

[-] shortwavesurfer@monero.town 1 points 9 months ago

Yep, the function did the trick. My guess is it was being misread at execution as a variable and thats why it was breaking

[-] bizdelnick@lemmy.ml 3 points 9 months ago* (last edited 8 months ago)

Your mistake is that after variable substitution bash does not handle quoted strings, i.e. it does not remove single quotes from sed command line. If you really need this to happen, you have to use eval:

i1xmr=$(echo "$i1p/$apiresponse*1000" | bc -l | eval $rmdec)

However using functions is a better solution in general. But in this particular case, I guess, you only need to change the bc's scale instead of using sed:

i1xmr=$(echo "scale=17; $i1p/$apiresponse*1000" | bc -l)

For better readability you may use heredoc instead of echo:

i1xmr=$( bc -l << EOF
scale=17
$i1p/$apiresponse*1000
EOF
)
[-] shortwavesurfer@monero.town 1 points 9 months ago

Oh dear Lord, and see, this is why I do not code for a living. What I ended up doing is using a function like this

  • rmdec() { sed 's/..................$//'; }
  • i1xmr=$(echo "$i1p/$apiresponse*1000" |bc -l |rmdec)
[-] shortwavesurfer@monero.town 2 points 9 months ago
[-] bizdelnick@lemmy.ml 1 points 9 months ago* (last edited 9 months ago)

Again, you don't need sed for this, simply set scale=2 or how many digits after decimal point you need. Also you missed ! in the shebang (#!/bin/bash or #!/bin/sh).

[-] shortwavesurfer@monero.town 1 points 9 months ago

So I need five digits after the decimal point, but then when I do the multiplication, I only want four digits in total. I did move "scale=" to five and that made the sed command in rmdec much shorter, but its still needed. I thought i could add "length=4" but that throws an error.

[-] bizdelnick@lemmy.ml 1 points 9 months ago

Multiply before you divide to keep precision: 1000 * $i1p / $apiresponse

[-] shortwavesurfer@monero.town 1 points 9 months ago

Ah, that did the trick. Thanks

[-] bizdelnick@lemmy.ml 1 points 9 months ago* (last edited 9 months ago)

And again, using here-document greatly improves readability, like this.

[-] shortwavesurfer@monero.town 1 points 9 months ago

Hmm. I had a look at the example given. I see the idea, but would cat be the thing to use or would it be echo <<-EOF > "$file"?

[-] bizdelnick@lemmy.ml 1 points 9 months ago

You need cat because it reads stdin and prints it to stdout. echo does not read stdin, it prints its arguments.

[-] shortwavesurfer@monero.town 1 points 9 months ago

I have never used cat like that before. If you just ent cat abcd > file it says abcd doesnt exist but does create "file". I know you can cat contents of a file into another file but why the <<-EOF > file works is a bit beyond me.

[-] bizdelnick@lemmy.ml 1 points 9 months ago

cat does not create file, your shell does when you redirect the standard output with > file.

[-] shortwavesurfer@monero.town 1 points 9 months ago

Alright, I modified it and formatted it. However, for whatever reason, the output HTML in /var/www/html/index.html does not keep the formatting and is all just left aligned as before. That's not really a problem, just more of a curiosity as to why it did not inherit the formatting of the input.

[-] bizdelnick@lemmy.ml 1 points 9 months ago* (last edited 9 months ago)

When using "<<-", shell removes all tabs from the beginning of each line. So you have to use tabs for formatting inside your script and then spaces for HTML formatting, as in my example. Or use "<<" without dash to preserve tabs.

this post was submitted on 24 Oct 2023
7 points (100.0% liked)

Programming

13097 readers
1 users here now

All things programming and coding related. Subcommunity of Technology.


This community's icon was made by Aaron Schneider, under the CC-BY-NC-SA 4.0 license.

founded 1 year ago
MODERATORS