Bash construct to extract the two values from commands like:"--option=false"? - bash

Does anyone know the bash construct where you specify the delimeter and you get in two variables ( $# or $! or something like that I think ) the values?
For example:
--option=false should be specified with '=' delimeter and in one variable there will be the word 'option' whereas in the other variable the word 'false' is stored. Anyone know?
P.S.: No sed, awk or IFS solutions please. I am aware of them but I am requesting the specific bash construct :)

As found in the Bash man page, you can use Parameter Expansion to solve your problems.
# split first argument on equal sign (left=right)
left=${1%%=*}
right=${1#*=}

I believe the GNU getopt program is appropriate here (if available, of course).
It supports long options.

Related

Combine two expression in Bash

I did check the ABS, but it was hard to find a reference to my problem/question there.
Here it is. Consider the following code (Which extracts the first character of OtherVar and then converts MyVar to uppercase):
OtherVar=foobar
MyChar=${OtherVar:0:1} # get first character of OtherVar string variable
MyChar=${MyChar^} # first character to upper case
Could I somehow condense the second and third line into one statement?
P.S.: As was pointed out below, not needs to have a named variable. I should add, I would like to not add any sub-shells or so and would also accept a somehow hacky way to achieve the desired result.
P.P.S.: The question is purely educational.
You could do it all-in-one without forking sub-shell or running external command:
printf -v MyChar %1s "${OtherVar^}"
Or:
read -n1 MyChar <<<"${OtherVar^}"
Another option:
declare -u MyChar=${OtherVar:0:1}
But I can't see the point in such optimization in a bash script.
There are more suitable text processing interpreters, like awk, sed, even perl or python if performance matters.
You could use the cut command and put it in a complex expression to get it on one line, but I'm not sure it makes the code too much clearer:
OtherVar=foobar
MyChar=$(echo ${OtherVar^} | cut -c1-1) # uppercase first character and cut string

How to access a variable that its name is made up by various strings in Shell Script?

So, this is similar to passing parameter by reference. I want to access a variable (echo) that its name is a combined string from different strings. A simple example would be the following:
A1=999
n="1"
B="A$n"
What I want is that when I do echo $B, it would return 999. Please let me know if further explanation is required. Thanks.
You are looking for indirection
echo ${!B}
From the bash manual
${!prefix*}
${!prefix#}
Expands to the names of variables whose names begin with prefix,
separated by the first character of the IFS special variable.
You could also do this
eval echo "$"$B
But Kevin's answer is definitely better.

Trouble understanding parameter substitution in a script

I'm trying to understand a bash script whose first four lines are:
#!/bin/sh
SCRIPT="`basename $0 | sed 's/\..*$//'`"
CONFIG=${1:-$HOME/.$SCRIPT}
DIR=${2:-$HOME/Documents}
I understand that the last two lines are doing parameter substitution on paths input as script arguments 1 and 2, but I've been unable to figure out how this works (e.g. here). What does the ":-" part mean? Sorry for the newbie question.
From man bash:
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted. Other‐
wise, the value of parameter is substituted.
Very easy to find, with man bash, and then /:-. The slash introduces a search, and :- is just the content to search for. Else, searching in bash can get very boring, because it is huge, but here it is the first hit.

Tricky brace expansion in shell

When using a POSIX shell, the following
touch {quick,man,strong}ly
expands to
touch quickly manly strongly
Which will touch the files quickly, manly, and strongly, but is it possible to dynamically create the expansion? For example, the following illustrates what I want to do, but does not work because of the order of expansion:
TEST=quick,man,strong #possibly output from a program
echo {$TEST}ly
Is there any way to achieve this? I do not mind constricting myself to Bash if need be. I would also like to avoid loops. The expansion should be given as complete arguments to any arbitrary program (i.e. the program cannot be called once for each file, it can only be called once for all files). I know about xargs but I'm hoping it can all be done from the shell somehow.
... There is so much wrong with using eval. What you're asking is only possible with eval, BUT what you might want is easily possible without having to resort to bash bug-central.
Use arrays! Whenever you need to keep multiple items in one datatype, you need (or, should use) an array.
TEST=(quick man strong)
touch "${TEST[#]/%/ly}"
That does exactly what you want without the thousand bugs and security issues introduced and concealed in the other suggestions here.
The way it works is:
"${foo[#]}": Expands the array named foo by expanding each of its elements, properly quoted. Don't forget the quotes!
${foo/a/b}: This is a type of parameter expansion that replaces the first a in foo's expansion by a b. In this type of expansion you can use % to signify the end of the expanded value, sort of like $ in regular expressions.
Put all that together and "${foo[#]/%/ly}" will expand each element of foo, properly quote it as a separate argument, and replace each element's end by ly.
In bash, you can do this:
#!/bin/bash
TEST=quick,man,strong
eval echo $(echo {$TEST}ly)
#eval touch $(echo {$TEST}ly)
That last line is commented out but will touch the specified files.
Zsh can easily do that:
TEST=quick,man,strong
print ${(s:,:)^TEST}ly
Variable content is splitted at commas, then each element is distributed to the string around the braces:
quickly manly strongly
Taking inspiration from the answers above:
$ TEST=quick,man,strong
$ touch $(eval echo {$TEST}ly)

Search and replace in Shell

I am writing a shell (bash) script and I'm trying to figure out an easy way to accomplish a simple task.
I have some string in a variable.
I don't know if this is relevant, but it can contain spaces, newlines, because actually this string is the content of a whole text file.
I want to replace the last occurence of a certain substring with something else.
Perhaps I could use a regexp for that, but there are two moments that confuse me:
I need to match from the end, not from the start
the substring that I want to scan for is fixed, not variable.
for truncating at the start: ${var#pattern}
truncating at the end ${var%pattern}
${var/pattern/repl} for general replacement
the patterns are 'filename' style expansion, and the last one can be prefixed with # or % to match only at the start or end (respectively)
it's all in the (long) bash manpage. check the "Parameter Expansion" chapter.
amn expression like this
s/match string here$/new string/
should do the trick - s is for sustitute, / break up the command, and the $ is the end of line marker. You can try this in vi to see if it does what you need.
I would look up the man pages for awk or sed.
Javier's answer is shell specific and won't work in all shells.
The sed answers that MrTelly and epochwolf alluded to are incomplete and should look something like this:
MyString="stuff ttto be edittted"
NewString=`echo $MyString | sed -e 's/\(.*\)ttt\(.*\)/\1xxx\2/'`
The reason this works without having to use the $ to mark the end is that the first '.*' is greedy and will attempt to gather up as much as possible while allowing the rest of the regular expression to be true.
This sed command should work fine in any shell context used.
Usually when I get stuck with Sed I use this page,
http://sed.sourceforge.net/sed1line.txt

Resources