Problems with bash variables

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

Problems with bash variables

Postby Munty Scruntfundle » 2018-11-27 17:31

Hi there. I'm new here, I can't find a bash forum so I hope this is in the right place.

I'm having some right old issues trying to get input/output to/from variables in Rasbian bash. I know Raspbian is a fork and therefore may not at all be the same, but surely it can't be far off yet.

The problem:

I'm trying to get the pre and post characters around a '@' character of a string. A user and ip address, example; user@192.168.xxx.xxx
Nothing odd there.

I've been googling for hours and trying all sorts of crazy things, but can't get what I need. I've whittled away a lot of examples I've tried and I'm currently working with the following:

If I:
-# command=$1 - $1 would be 'user@192.168.xxx.xxx'
-# echo $command | cut -d'@' -f1
returns 'user'
but
-# command=$1
-# u=$command | cut -d'@' -f1
-# echo $u
returns an empty line with a cr.

I've tried putting my variables in quotes, single quotes, brackets, curly brackets, square brackets, all the different google syntax I can find and the answer eludes me.

Could someone please explain why it's doing what it is, and how it should work?

Many thanks.
Munty Scruntfundle
 
Posts: 5
Joined: 2018-11-27 16:53

Re: Problems with bash variables

Postby Head_on_a_Stick » 2018-11-27 17:49

Does this help:
Code: Select all
empty@buster:~ $ command='user@192.168.1.1'
empty@buster:~ $ prefix=${command%@*}
empty@buster:~ $ echo $prefix
user
empty@buster:~ $ suffix=${command#*@}
empty@buster:~ $ echo $suffix
192.168.1.1
empty@buster:~ $

http://wiki.bash-hackers.org/syntax/pe
dbruce wrote:Ubuntu forums try to be like a coffee shop in Seattle. Debian forums strive for the charm and ambience of a skinhead bar in Bacau. We intend to keep it that way.
User avatar
Head_on_a_Stick
 
Posts: 8322
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Problems with bash variables

Postby Munty Scruntfundle » 2018-11-27 19:41

I worked it out in the end. Bloomin brackets and spaces!
Munty Scruntfundle
 
Posts: 5
Joined: 2018-11-27 16:53

Re: Problems with bash variables

Postby Munty Scruntfundle » 2018-11-27 19:42

Does this help:
CODE: SELECT ALL
empty@buster:~ $ command='user@192.168.1.1'
empty@buster:~ $ prefix=${command%@*}
empty@buster:~ $ echo $prefix
user
empty@buster:~ $ suffix=${command#*@}
empty@buster:~ $ echo $suffix
192.168.1.1
empty@buster:~ $

http://wiki.bash-hackers.org/syntax/pe


OH YES, very much so, that's a lot cleaner. Thank you.
When I'm a little happier with it I'll post it up and you can all shoot my methodology down in flames!!
Cheers.
Munty Scruntfundle
 
Posts: 5
Joined: 2018-11-27 16:53

Re: Problems with bash variables

Postby debiman » 2018-11-28 06:54

i think you should use a wiki to learn bash scripting from scratch instead of opening a thread for every hurdle you encounter.

i took your other request seriously (ssh and sudo su), now i feel a little miffed because i realise you're just faffing around to learn bash.
User avatar
debiman
 
Posts: 3064
Joined: 2013-03-12 07:18

Re: Problems with bash variables

Postby Munty Scruntfundle » 2018-11-28 18:51

Ohh no no, I'm certainly not faffing. I'm learning with the serious intent to learn.

Following a road accident 7 years ago I don't do a lot of going out, I don't work any more, there are a lot of hobbies I've had to leave behind and many days it's all I can do to struggle into a wheelchair. I used to run my own company providing turn key bar coding systems for smes, nhs, automotive, a lot of asset tracking and stock control written predominantly with Visual Studio and odd printer code, plcs and interfaces. That had to go as I can't carry or travel any more. So I decided I'd get into apple and android and unix and raspberry pis and all sorts of craziness.

The current project is a 128 raspberry pi cluster which hopefully will soon be running all kinds of mpi projects and cluster rendering and whatever else I can throw at it. The ultimate aim is to open the cluster to the outside world, let people dial in and run test code. Clusters are in great demand.

Your help is extremely helpful and welcome, and I thank you for your ideas. I do wholeheartedly agree, I need to read a complete bash manual and learn it all properly, I just haven't had much time to work on this lately. I will find the time and I will start to contribute to the forum. Can you recommend any good sources? A quick reference book with examples?

So no, you haven't helped a working desk person get a gold star, but you did set me off looking at cutting strings in ways I would not have found for weeks.

I thank you once again. :o)
Munty Scruntfundle
 
Posts: 5
Joined: 2018-11-27 16:53

Re: Problems with bash variables

Postby xepan » 2018-11-28 20:43

Munty Scruntfundle wrote: Can you recommend any good sources? A quick reference book with examples?

http://mywiki.wooledge.org/
The guide and the FAQ for learning, the pitfalls to avoid mirroring the usual stuff you will find on the web.
http://wiki.bash-hackers.org/

-# u=$command | cut -d'@' -f1
...
Could someone please explain why it's doing what it is, and how it should work?

Parameter Expansion mentioned above is the proper solution, but if you wanted to do it the way you did, you would need "command substitution". I only mention it so you see why your approach didn't work, and it might be handy for other problems
Code: Select all
u=$(echo command | cut -d'@' -f1)

http://mywiki.wooledge.org/CommandSubst ... pansion%29

On another note: "command" is a shell builtin. I avoid giving variables names which are already "used" (not saying it causes trouble, i simply don't know, but it sure adds confusion).
xepan
 
Posts: 14
Joined: 2018-11-28 06:38


Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable