LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2022, 04:20 AM   #16
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled

Quote:
Originally Posted by blumenwesen View Post
I want split the string from special position.

05.01.2022 date 1

to

05.01.2022 date
|
05.01.2022
|
05.01.
|
05.
|
05
Quote:
i need the length to cut.
Why?
Code:
#!/bin/sh
d='05.01.2022 date 1'
echo "$d"
echo "${d% *}"
echo "${d%% *}"
echo "${d%.*}".
echo "${d%%.*}".
echo "${d%%.*}"

Last edited by shruggy; 01-19-2022 at 04:29 AM.
 
Old 01-22-2022, 07:48 AM   #17
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
Ok thank you, but is it unable thats work like this?

Code:
z="abcd"; a="d"
echo ${z:${z%%$a*}0:-1}
echo ${z:${z%%$a*}>2?0:-1}
 
Old 01-22-2022, 08:57 AM   #18
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Again. The part between colons must resolve to a number.
Code:
z="abcd"; a="d"
echo ${z:${z%%$a*}0:-1}
This is what happens here

1. ${z%%$a*} resolves to abc;
2. Bash takes abc as the name of a (numeric) variable;
3. If this variable is not defined (unset), it will expand to zero according to the shell arithmetic rules;
4. The result will be abc.

The result would be different in this case:
Code:
z=abcd;a=d;abc=1
echo ${z:${z%%$a*}:-1}
Can you tell why?
 
Old 01-22-2022, 11:09 AM   #19
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
I already knew it is similar in the prinziep with many programming languages with the length limit, would of course fit better here.

Code:
z[@]
echo ${z:0:${z%%$a*}[@]-1} Fieldindex Error
This was just an example, I do not understand how the variable can be replaced at the beginning by the result and the curly brace can be defined in a curly bracket.

That's just an example, I don't understand how the variable at the beginning can be exchanged for the result and code in the curly brace can be defined inside a curly brace.

How can I integrate the first fragment of code into the second one without going through the variable specification?
If it's impossible, there is no length limit imposed by numbers can be defined?

Code:
y=${z%%$a*}
echo ${y:0:-1}
unplace to
${${z%%$a*}:0:-1}
or
${z%%$a*:0:-1}
or
${(${z%%$a*}):0:-1}

Last edited by blumenwesen; 01-22-2022 at 11:13 AM.
 
Old 01-22-2022, 11:20 AM   #20
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by blumenwesen View Post
Code:
${${z%%$a*}:0:-1}
or
${z%%$a*:0:-1}
or
${(${z%%$a*}):0:-1}
None of these is possible in Bash.

What's your problem with having it in two statements?
 
Old 01-22-2022, 04:33 PM   #21
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
Variable definition if not absolutely necessary often leads to it becoming confusing and it takes up memory, if several processes are to run a program as a starter and service, it can be a little more code and longer with old pcs.

Then, by combining code fragments in curly brackets, can also understand connections and syntax.
 
Old 01-23-2022, 02:23 AM   #22
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
Quote:
Originally Posted by blumenwesen View Post
Variable definition if not absolutely necessary often leads to it becoming confusing and it takes up memory, if several processes are to run a program as a starter and service, it can be a little more code and longer with old pcs.

Then, by combining code fragments in curly brackets, can also understand connections and syntax.
No, that is just speculation. Writing unreadable code makes hard to understand/maintain it. Usually an additional variable has no any cost in speed and memory usage (especially when it holds only a few bytes) or at least it is extremely hard to measure and demonstrate. And bash is actually a slow language, this kind of "improvement" is completely futile.
Understandability depends on the structure of the code, the used function and variable names, not on the number of lines and curly brackets. Also you may add comments if you wish to explain...
 
Old 01-23-2022, 05:37 AM   #23
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
I can read it better that way, and write in python as a starter without a paging file is code that has to be written very laboriously because the different quotation marks and special characters have to be taken into account several times, the python code is also more than double so long and taking longer to run because of the loading of the additional libraries that bash has direct access to.
Python libraries are constantly changing and therefore custom code would need to be updated pointlessly over and over again, especially if you still use 2.7.
 
Old 01-24-2022, 12:57 AM   #24
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,800

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The Posix standard defines ${ to be a variable modifier
That means it must be followed by the name of a variable, ${var

zsh (but not bash) has extended this to a value modifier, and the value may be any sub-expression.
So you can give zsh a try.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash question on nested braces CelticFiddler Linux - Newbie 11 06-30-2022 07:32 AM
LXer: All about {Curly Braces} in Bash LXer Syndicated Linux News 0 02-28-2019 12:30 AM
[SOLVED] Undefined variable inside Curly braces in the function srinietrx Programming 4 10-13-2015 02:38 AM
Unexpected curly braces in expect script spawn command & bash suid problem slinx Programming 1 05-02-2008 01:47 PM
Curly Modem Problem stefanvg Linux - Hardware 0 04-28-2003 01:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration