Linux bash get an input on the same line as execution? [duplicate] - linux

This question already has answers here:
Script parameters in Bash
(5 answers)
How do I parse command line arguments in Bash?
(40 answers)
Closed 2 years ago.
I made a .sh file with a program, the current input is as follows:
$ ./myprogram.sh
file.txt
How can I make it so the input is as follows instead:
$ ./myprogram.sh file.txt

Inside the shell script, you can refer to the arguments by their positions as $1, $2, etc. Note that the arguments start at 1, and $0 is the name of the executed script. $# contains the total number of arguments.

Related

Why set variable with output of bash command use only first line? [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 years ago.
I want to use:
schema=$(kubectl exec -n $namespace -it $podName -- bash -c "./spiral orm:schema")
echo $schema
But eventually in schema variable recorded only the first line from the result of bash execution.
How to make it use all lines?

Bash how to store in a variable the result of a linux command? [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 2 years ago.
I'm trying to store in a variable the temperature of the computer. I tried this but it doesn't work:
#!/bin/bash
temp = cat "/sys/class/thermal/thermal_zone0/temp"
echo "$temp"
i tried this too:
#!/bin/bash
temp = $(cat "/sys/class/thermal/thermal_zone0/temp")
echo "$temp"
but nothing works, it always says
./temp.sh: line 2: temp: command not found
Spaces are crucial! This works fine:
# NO space around `=`
temp=$(cat "/sys/class/thermal/thermal_zone0/temp")
echo "$temp"

How to source a key value paired file in bash escaping whitespace? [duplicate]

This question already has answers here:
Use key/value data from a file in a shell script
(1 answer)
Reading key/value parameters from a file into a shell script
(1 answer)
Closed 3 years ago.
$ cat foo.txt
a=1one
b=2two
c=3 three
d=4four
$ source foo.txt
bash: three: command not found...
Need to set all the variable listed in foo.txt, how to source this file by escaping the space character? foo.txt comes from other application, which I cannot control, or is there an alternative to source ?
If the output is so regular, you could try to preprocess the file using sed like this:
$ sed -e "s/=/='/;s/$/'/" < foo.txt >sourced.env
and then source sourced.env. This will add a ' just after the = and add an ending '.

Concatenating multiple text files by arguments in a script into a single file in Bash [duplicate]

This question already has answers here:
How to cat multiple files from a list of files in Bash?
(6 answers)
Closed 4 years ago.
How should i concatenate multiple text files get by arguments in terminal using a script in Bash?
#!/bin/bash
while read $1
do
cat $1 > cat.txt
done
I tried that example but it is not working.
You should use '>>' to concatenate ('>' will create a new file each time):
for file in "$#"
do
cat $file >> result
done

Shell script variable value not getting update [duplicate]

This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Dynamic variable names in Bash
(19 answers)
Is it possible to build variable names from other variables in bash? [duplicate]
(7 answers)
Closed 5 years ago.
I'm trying to execute below bash shell script, but not getting the expected output. Possible i'm doing something wrong or it's not the way of doing this.
#bin/bash
#set -x
path1_one=/home/dell/scripts
echo $path1_one
param_val=path1_one
param1=$( echo "$param_val" | awk -F '_' '{ print $0 }' )
#path2="$path1"
echo $param1
#echo $path2
Output:
/home/dell/scripts
path1_one
Expected Output:
/home/dell/scripts
/home/dell/scripts
Both variable value should be same,but don't know why param1 value is not reflecting with path1_one
You need to tell the script that you want to use the value of the variable path1, not the name path1.
Use:
path2="$path1"

Resources