Using bash read command to retrieve multiple words [duplicate] - bash

This question already has an answer here:
Using a variable containing spaces as a single argument
(1 answer)
Closed 8 years ago.
I am writing a bash script and i have the following:
#!/bin/bash
echo Enter some text
read tweet
t update $tweet
I have sferik t installed to tweet using the t update command and i want to pass multiple words into the $tweet variable however i am reciving this message.
ERROR: "t update" was called with arguments ["sdfs", "sdfsdf"]
Usage: "t update [MESSAGE]"

You need to quote the variable so it's passed as a single argument to t:
t update "$tweet"
In general, always quote your variables unless you know exactly why you should not.

Related

Composing a string in script shell [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 1 year ago.
So I'm trying to compose a string using script shell
#!/bin/sh
blNumber=1111619832
echo '*'$blNumber+='*.xlsx'
I expect the output to be: *1111619832*.xlsx
but as a result I get: *+=*.xlsx
Btw I tried to figure out why so I tried this code
#!/bin/sh
blNumber=1111619832
output="*${blNumber}"
and whenever I add something after *${blNumber} it get concatenated at the begging of the string
Why are you using += in the first place?
$ echo '*'$blNumber'*.xlsx'
*1111619832*.xlsx
Or put it inside double-quotes. It's best practice to quote all variables anyway.
$ echo "*$blNumber*.xlsx"
*1111619832*.xlsx

How to add quoted parameters to a quoted command in a shell script? [duplicate]

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Closed 2 years ago.
I want to compose a command in a shell script like this:
#!/bin/sh
APPLICATION="date"
PARAMETER="-d '2020-01-01 1:23'"
CMD="${APPLICATION} ${PARAMETER}"
${CMD}
The 'PARAMETER' is supposed to hold parameters that need to be quoted themself. Unfortunately it does not work like this. Escaping them via PARAMETER="-d \"2020-01-01 1:23\"" also does not work.
After you've build CMD up, it is just string. It contains what can be interpreted by you as a command, but the shell sees it as a bare string.
If you want the string to reinterpret it, you need to eval it:
eval "$CMD"
However, eval is often considered evil.

Infinite User Input Variables In Bash? [duplicate]

This question already has answers here:
How to define a shell script with variable number of arguments?
(3 answers)
Closed 4 years ago.
Currently, I'm working on a Bash script that would allow for an infinite amount of input from the user.
Currently, I have the script running only with a specific number of variables.
I'm using the following line:
read var1 var2 var3
My goal is to have it so that the user can input as many variables into the script without having to add a bunch of variables
Example of use
read -a var # see help -m read
echo "${#var[#]} inputs in array \$var with indexes: ${!var[#]}"
echo "inputs: ${var[#]}"

Replacing substring with other text in variable inside a bash script [duplicate]

This question already has answers here:
Replace one substring for another string in shell script
(16 answers)
Closed 5 years ago.
I've been trying to write a bash script. A part of it is supposed to replace a part of a string with nothing.
Here's what I'm trying to do
$dbname=$1
$dbNameActual="${$dbname/.sql/}"
date
echo $dbNameActual
I tried a number of suggestions from stack. But got nowhere. I tried adding sed, but that didn't seem to work.
The idea is that I have a script, and it takes in a db import file name, say db250317.sql and outputs db250317 .
I'm running Ubuntu 16.04 LTS.
You don't put $ twice in the expression, and you don't put $ before the variable you're assigning to (this isn't PHP or Perl). It should be:
dbNameActual="${dbname/.sql/}"
Also, if the thing you're trying to delete is always at the end, you can use % to remove it:
dbNameActual="${dbname%.sql}"
Also remember to quote the variable when you use it later, in case the filename contains spaces. You should almost always quote variables, unless you have a specific reason not to.

How can i get the value from a file using bash? [duplicate]

This question already has answers here:
Shell command to retrieve specific value using pattern
(3 answers)
Closed 8 years ago.
I have file test.txt contains the following
AA=testing
BB=help
CC=hello
How can i make a bash script that will get each value and assign to a new variable?
#!/bin/bash
var1=testing
var2=help
var3=hello
thanks for the help
First of all a = value is not correct syntax in shell. In shell the spaces are important.
When you have a valid file, you can use the eval function to evaluate that file as a string, or simply source it.

Resources