[SOLVED] Function script in terminal

Need help with C, C++, perl, python, etc?

[SOLVED] Function script in terminal

Postby Ignition » 2018-12-15 12:17

Hello everybody!! :D

Thanks for passing by, I'm having troubles creating functions directly in the shell. I am following a tutorial where it explains how to make a function to prepend environment variables. It shows how to mae the script:

Code: Select all
root@Zion:~# prepend() { [ -d "$2" ] && eval $1=\"$2':'\$$1\" && export $1; }


So then I would be able to use the function to add an specific path to $PATH.

In the tutorial, obviously, worked fine, but when I declare the function, a new line appears, specting to write something else. Like if I haven't finish, and the only way to get out of it is pressing crtl+c, but, obviously, the function is not declare at all.

Code: Select all
root@Zion:~# prepend() { [ -d "$2" ] && eval $1=\"$2':'\$$1\" && export $1; }
>
>
root@Zion:~# prepend LD_LIBRARY_PATH /my/path
-bash: prepend: no se encontró la orden


Thanks a lot for your time!
Last edited by Ignition on 2018-12-16 12:55, edited 1 time in total.
Ignition
 
Posts: 11
Joined: 2018-12-07 12:42

Re: Function script in terminal

Postby Head_on_a_Stick » 2018-12-15 13:07

Works for me:
Code: Select all
bash-4.4$ unset LD_LIBRARY_PATH
bash-4.4$ echo $LD_LIBRARY_PATH

bash-4.4$ prepend() { [ -d "$2" ] && eval $1=\"$2':'\$$1\" && export $1; }
bash-4.4$ prepend LD_LIBRARY_PATH ~/Go
bash-4.4$ prepend LD_LIBRARY_PATH ~/git
bash-4.4$ echo $LD_LIBRARY_PATH
/home/empty/git:/home/empty/Go:
bash-4.4$ echo $BASH_VERSION
4.4.23(1)-release
bash-4.4$

This is under xterm in OpenBSD -current, what are you running?
User avatar
Head_on_a_Stick
 
Posts: 10471
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Function script in terminal

Postby xepan » 2018-12-15 15:12

weird. copy/paste your function works here too, and doesn't give
>
(which usually means a closing bracke, paranthesis, quote, etc is missing).

Does creating a more clear, most easy function work? I guess no, so your problem is somewhere else. Where? that is quite a question ...

eval is evil. I bet there are much easier solutions to the problem. Make sure the how-to is worth to follow.
xepan
 
Posts: 89
Joined: 2018-11-28 06:38

Re: Function script in terminal

Postby Ignition » 2018-12-16 12:54

xepan wrote:weird. copy/paste your function works here too, and doesn't give
>
(which usually means a closing bracke, paranthesis, quote, etc is missing).

Does creating a more clear, most easy function work? I guess no, so your problem is somewhere else. Where? that is quite a question ...

eval is evil. I bet there are much easier solutions to the problem. Make sure the how-to is worth to follow.


Well.... it seems that you are right. I've been cracking my head with this for a few days and, apparently, it was my own mistake, everytime I wrote the script, I wrote it wrong.

Thank you very much for your help, and sorry for the waste of time. :?

By the way.. Why you say eval is evil?? It's not an effective function?
Ignition
 
Posts: 11
Joined: 2018-12-07 12:42

Re: Function script in terminal

Postby xepan » 2018-12-16 15:46

Ignition wrote:By the way.. Why you say eval is evil?? It's not an effective function?

To be honest: I can't tell you. I remember common recommendations, and that pretty well, but often i can't say *why* it is recommended to do it.
As far eval is concerned: i never had the feeling i would need it ( besides "eval $(ssh-agent -s)" ), so i didn't investigate at all (gotta say that i got really hard times to understand what the heck "eval" does at all.

Long story short, here is a link, giving by the bot <greybot> of the IRC #bash channel:
<greybot> 'eval' is a common misspelling of 'evil'. If eval is the answer, surely you are asking the wrong question. See
http://mywiki.wooledge.org/BashFAQ/048


More short: i don't know, and was talking way over my head, hoping to get away with it :-)
xepan
 
Posts: 89
Joined: 2018-11-28 06:38

Re: [SOLVED] Function script in terminal

Postby Ignition » 2018-12-16 16:21

Thank you very much for the info!!

It is good to be aware of the risks that can bring use some specific code.

Really useful!
Ignition
 
Posts: 11
Joined: 2018-12-07 12:42

Re: [SOLVED] Function script in terminal

Postby xepan » 2018-12-16 16:32

from wherever you learn, i think it is a good idea to make #bash IRC confirm that the solution you ran into isn't "wrong" (comes with drawbacks, etc). I just idle there and hear things (as said: some i remember without even understanding them).
Also try http://shellcheck.net and compare whatever you are getting taught with the website from above: mywiki.wooledge.org, mainly the pitfalls: http://mywiki.wooledge.org/BashPitfalls
They are a bit very nitty-gritty, but if you use the web, then you run in the most weird "solutions" (say you will run in someone like me :-) ).
<greybot> Google is NOT a preferred source for learning bash, because almost all the "tutorials" and scripts out there are JUNK. Instead, ask a good question here or refer to the Guide and FAQ (see topic)

which sounds very harsh, but once you read a bit of the pitfalls, you will be astonished what you find with a google search.
xepan
 
Posts: 89
Joined: 2018-11-28 06:38

Re: [SOLVED] Function script in terminal

Postby Ignition » 2019-01-07 13:20

xepan wrote: Also try http://shellcheck.net and compare whatever you are getting taught with the website from above: mywiki.wooledge.org, mainly the pitfalls: http://mywiki.wooledge.org/BashPitfalls


Thanks for this tools. I'm going to study the pitfalls carefully, so I don't increase bad habits.

<greybot> Google is NOT a preferred source for learning bash, because almost all the "tutorials" and scripts out there are JUNK. Instead, ask a good question here or refer to the Guide and FAQ (see topic)


I agree with this. I came up with some really bad tutorials and "courses" I just followed a guide to refresh some knowledges I had and see what is coming. At this point I'm developing some little things to get used to the code.

Thanks again for your help and advise!
Ignition
 
Posts: 11
Joined: 2018-12-07 12:42


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable