Quick question about Environment Variables use in SH scripts - shell

I have found a script to set the background image and do some more stuff, but that's not important.
Look at this line, it's supposed to set a route, nothing complicated.
#!/bin/sh
bgloc="${XDG_DATA_HOME:-$HOME/.local/share/}bg"
I didn't understand what was it doing, because of the env variable and the :- that comes after it. Now, after some trial and error, I've figured it out. It sets the variable bgloc with the value of that env variable and, if that variable is not set —which is the case—, then, it use the route that comes after the :- .
So, the question is: why does that happen? I mean, I've searched it on the internet, and I haven't found anything. Is it supposed to work like this? In that case, where can I find information about that? I'd like to learn more about it, but I don't find any information on the internet. Some help would be great. Thanks.

A lot can be done with values within ${} in bash.
To find out more about these features I would recommend to read TLDP manual on this topic.

Related

How to hide the resolved value of a variable in ksh?

I am currently stuck with a situation where i need to hide the resolved value of a variable, i.e., the value should not appear when the code runs in the debug mode i.e., ksh -x.
I have seen other threads on similar kind of problem but there, a way has been provided when the value is read from STDIN, with the help of read -s option. But i do not have to read the value from STDIN.
Kindly help me with this.
Thanks,
Amit
When the field to be hidden is unique (something like a strong password), you can make a wrapper that changes the output of the script.
You should only take care for the special characters in the variable such as a slash. Try something like
mycode.sh 2>&1 | sed "s/${myvar}/xxxxxxxx/g"
When you use the variable on a few places only, a good alternative is testing the mode you are running in, and turn off the debug mode before using the variable (and turn on one line later).

Custom Input For Command Prompt?

What I'm looking for seems to be very simple. I want to run a command prompt line, but have it to be easily editable for anyone to use. There are multiple commands to this script, but only one part needs to be changed, and asks the user for a custom input. I'm sure this is already online, however after looking for hours and finding nothing as I have no clue what it would even be called, this is my last resort. Any help would be appreciated as I'm not even quite sure how to describe this.
Your question is a bit vague but if I understand correctly it sounds like you are trying to read user input (ie. prompt the user for input) from a batch file. If so, this should work for you:
set /p CustomInput="Please enter blah blah blah.."
Then you can use %CustomInput% as a variable in the script.
I guess you may also add this line:
goto %CustomInput%

Extending tcsh completion

I must work with tcsh.
I am using an internal tool that provides basic completion for some of its commands.
I would like to extend the completion.
I mean that in future releases the default completion may evolve.
I tried something like this:
set def_cmpl = complete tool
complete tool $def_cmpl 'n/-l/(reg short long gui)/'
But I don't understand the result I get.
Indeed, the quotes inside $def_cmpl are doubled:
tcsh> complete tool
''n#-t#$script#'' n/-l/(reg short long gui)/'
I tried some tricks with echo, sed, etc. but I can't avoid those ''.
Could somebody help me?
Please don't say go on bash... The tool doesn't support it...
Finally, I did not find a solution to keep the data inside the script. So, the solution was to redirect the output of the complete command inside a file and then to append new lines to the file.

JMeter mathematical function

I have one random variable and one variable which I read from page. How can I subtract or multiply that two variables and where or in which component in JMeter.
Check out the functions page for Jmeter. It has tons of cool math tools that you can plug anywhere in your script.
You'll most likely end up doing a jexl command, which would look something like this: ${__jexl2(${var1}-${var2})}
The above did not work for me. How ever i was able to resolve my problem with :
${__jexl(vars.get("YOUR_VARIABLE")+1)}

Is it possible to add multiple commands to the readline .inputrc file?

I'm trying to configure my Terminal and I would like to insert #{} at one key-stroke. This works with the following code
# .inputrc
"\e\"": "#{}"
But I also want the cursor to end up inside the braces. How can I do this? The following doesn't work.
# .inputrc
"\e\"": "#{}": backward-char
Try:
"\e\"": "#{}\e[D"
My immediate way to fix your overall goal (not really answering your question, but hopefully helping you anyway): write a bash alias or function for it. grev() perhaps, or something similar - at least, this is what I would do were I in your situation.
I am interested to see if what you originally asked is possible, however, so voting up your question in hopes that you can get a 'real answer'!

Resources