Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[SOLVED] Function script in terminal

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
Ignition
Posts: 11
Joined: 2018-12-07 12:42

[SOLVED] Function script in terminal

#1 Post by Ignition »

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.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: Function script in terminal

#2 Post by Head_on_a_Stick »

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?
deadbang

xepan
Posts: 89
Joined: 2018-11-28 06:38

Re: Function script in terminal

#3 Post by xepan »

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.

Ignition
Posts: 11
Joined: 2018-12-07 12:42

Re: Function script in terminal

#4 Post by Ignition »

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?

xepan
Posts: 89
Joined: 2018-11-28 06:38

Re: Function script in terminal

#5 Post by xepan »

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 :-)

Ignition
Posts: 11
Joined: 2018-12-07 12:42

Re: [SOLVED] Function script in terminal

#6 Post by Ignition »

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!

xepan
Posts: 89
Joined: 2018-11-28 06:38

Re: [SOLVED] Function script in terminal

#7 Post by xepan »

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.

Ignition
Posts: 11
Joined: 2018-12-07 12:42

Re: [SOLVED] Function script in terminal

#8 Post by Ignition »

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!

Post Reply