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.
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
Problems with bash variables
- Head_on_a_Stick
- Posts: 14114
- Joined: 2014-06-01 17:46
- Location: London, England
- Has thanked: 81 times
- Been thanked: 133 times
Re: Problems with bash variables
Does this help:
http://wiki.bash-hackers.org/syntax/pe
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:~ $
deadbang
-
- Posts: 54
- Joined: 2018-11-27 16:53
-
- Posts: 54
- Joined: 2018-11-27 16:53
Re: Problems with bash variables
OH YES, very much so, that's a lot cleaner. Thank you.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
When I'm a little happier with it I'll post it up and you can all shoot my methodology down in flames!!
Cheers.
Re: Problems with bash variables
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.
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.
-
- Posts: 54
- Joined: 2018-11-27 16:53
Re: Problems with bash variables
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. )
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. )
Re: Problems with bash variables
http://mywiki.wooledge.org/Munty Scruntfundle wrote: Can you recommend any good sources? A quick reference book with examples?
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/
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-# u=$command | cut -d'@' -f1
...
Could someone please explain why it's doing what it is, and how it should work?
Code: Select all
u=$(echo command | cut -d'@' -f1)
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).