Vindaar / shell

A Nim mini DSL to execute shell commands

Home Page:https://vindaar.github.io/shell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Is this possible? If so, how?

ITwrx opened this issue · comments

commented

Maybe it's just my poor understanding, but is it possible to do the following and if so, what is the correct syntax?

I'm trying to run:

sed '/\/home\/itwrx\/Documents\/blurts.css/d' /home/itwrx/Routines/file.txt

(the line to remove via sed being passed in as new_path and the file to remove it from being passed in as full_filepath)

using

shellAssign:
  sed_output = sed "'/"($new_path)"/d'" ($full_filepath)

and other (possibly closer to correct) variations.

Thanks

Given that the sed argument contains a ' (which is a character literal in Nim), we have to hand a raw string literal to shell to make it work (r"myString" is a raw string literal).
Note that you can always use the shellEcho macro to look at the actual command that is going to be handed to the shell:

import shell

let myPath = r"'/\/home\/itwrx\/Documents\/blurts.css/d'"

shellEcho:
  sed ($myPath) /home/itwrx/Routines/file.txt
  sed r"'/\/home\/itwrx\/Documents\/blurts.css/d'" /home/itwrx/Routines/file.txt

var sed_output: string
shellAssign:
  sed_output = sed ($myPath) /home/itwrx/Routines/file.txt

echo sed_output

This should be what you want if I understand correctly.

The first two lines give you the following output at compile time:

&"sed {myPath} /home/itwrx/Routines/file.txt"
"sed \'/\\/home\\/itwrx\\/Documents\\/blurts.css/d\' /home/itwrx/Routines/file.txt"

which is how each line will be handed to the call and then at runtime (after string interpolation etc.) this is output as:

sed '/\/home\/itwrx\/Documents\/blurts.css/d' /home/itwrx/Routines/file.txt
sed '/\/home\/itwrx\/Documents\/blurts.css/d' /home/itwrx/Routines/file.txt

So this should work. I'm not a super sed user, so I had to look up the meaning of your call in the sed bible:
https://www.grymoire.com/Unix/Sed.html#uh-27

Seems like the following should do the same no?

let myPathAlt = r"'\_/home/itwrx/Documents/blurts.css_ d'"

In general though if you have to use lots of funny characters when dealing with a shell command, the shell macro can make that even more "fun" unfortunately. That's just a limitation of abusing Nim macros. :)

Let me know if I misunderstood your question.

commented

thanks for your very detailed response.

Yes, you've got the right idea about what i'm trying to do, but the only thing is that both new_path and full_filepath are variables and are being passed into the proc (not shown here) as params, so i can't just use a string in the second part of the sed command as you did above, and i have to concatinate the new_path variable into the raw string literal for the first part of the sed command.

When i try:

var path_raw_literal = r"'/" & new_path & "/d'"

shellAssign:
  sed_output = sed ($path_raw_literal) ($full_filename)

I get:

Error when executing: sed {path_raw_literal} {full_filename}
err> sed: -e expression #1, char 3: extra characters after command

It seems like shell isn't including the full_filename part in the command. I'm not sure of the syntax needed to pull in both variables.

i also tried:

var raw_literal = r"'/" & new_path & "/d' " & full_filepath & ""

echo raw_literal

shellAssign:
  sed_output = sed ($raw_literal)

to try and simplify what is passed to shell, which echos:

'/\/home\/itwrx\/Documents\/xmas_list.txt/d' /home/itwrx/EZ-Bkup/Routines/default.txt

like it's supposed to, but gives:

Error when executing: sed {raw_literal}
err> sed: -e expression #1, char 0: unmatched `{'

so, so far i haven't found a way to use two variables,

Thanks for the more detailed explanation. The line Error when executing: sed {raw_literal} made me realize that there's apparently been a regression in shellAssign. The string interpolation is ignored in shellAssign. I'll fix it in a few hours after work!

#13 should have fixed this. Please update to version 0.4.2 and let me know if everything works. :)

commented

yes, it:

#vars new_path and full_filepath passed in as params to here unseen proc.

var sed_output = ""

var path_raw_literal = r"'/" & new_path & "/d' "  

shellAssign:
  sed_output = sed ($path_raw_literal) ($full_filepath)

works now! Thanks a lot for your help and work!

Is there somewhere where i can send you a small donation for this project and/or your beverage of choice? The more freedom and privacy-respecting the payment method, the better, with an monero/xmr address being ideal. :)

works now! Thanks a lot for your help and work!

Happy to hear everything works now!

Is there somewhere where i can send you a small donation for this project and/or your beverage of choice? The more freedom and privacy-respecting the payment method, the better, with an monero/xmr address being ideal. :)

I don't have any donation addresses anywhere, because I don't want people to feel like they should donate. Also with most projects I feel they "are not good enough" to deserve donations.
I'm coding for fun and my own use after all.
But since you explicitly ask for Monero... :)

monero:8B9PgRmaWuLgxCXYLC1DqqXUMKfE5ZDXwS2mFrb7vH7RgXTEz3fJ2wi7gNJ7TQSeZaSgMERQQWn7aAPVJJoHUfEeAfjsxh1

commented

Well, in that case you might consider something like

Don't feel obligated to donate in order to use this library, but if you want to support this work, and/or the support you received, i accept Monero at Monero:8B9PgRmaWuLgxCXYLC1DqqXUMKfE5ZDXwS2mFrb7vH7RgXTEz3fJ2wi7gNJ7TQSeZaSgMERQQWn7aAPVJJoHUfEeAfjsxh1

or something like that. We all feel like our work is not good enough at times. I always "punch above my weight" so i'll never feel like it's the best it can be.

Your library, it's thorough docs, and your helpful support made it possible for me to continue on with what i was trying to do. I wasn't sure how to do it with the standard library. It doesn't matter if it's just a small library. Small libraries can be important for the health of the ecosystem.

Thanks