How do I retrieve * literal using read command? [duplicate] - bash

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Printing asterisk ("*") in bash shell
(2 answers)
Closed 2 years ago.
I have a script that receives input from the user. One of the possible values I want to retrieve is the literal *. It seems the shell is intercepting it and executing so that I'm getting a list of all the file names.
while read -p "Enter operand: " operandTwo
echo "Argument: $operandTwo"
echo "Argument: $(echo $operandTwo)"
[ $operandTwo != X ]
echo "Result: $?"
do...
This is the output:
Argument: *
Argument: File1 File2 File3
./script: line 20: [: too many arguments
Result: 2
Is the comparison being made here between File1 File2 File3 and X? How do I make a comparison between * and X instead?

Related

Why is only the first line evaluated with IFS=' ' [duplicate]

This question already has answers here:
Unix read command with option -d and IFS variable combination
(2 answers)
What does the bash read -d '' do?
(2 answers)
Bash: assignment of variable on same line not altering echo behavior [duplicate]
(2 answers)
Bash variable assignment before command [duplicate]
(2 answers)
Closed 2 years ago.
I'm trying to split a multi-line string on spaces only, preserving line breaks:
IFS=' ' read a b c <<< "$(printf '%s\n' "foo" "bar" "baz")"; echo "a=[$a]"; echo "b=[$b]"; echo "c=[$c]"
Expected:
a=[foo
bar
baz
]
b=[]
c=[]
Actual:
a=[foo]
b=[]
c=[]
What am I missing? When I replace \n with \t it works as expected.
I'm using bash v5.0.18

Comparing two variables in IF condition inside While loop in bash script [duplicate]

This question already has answers here:
Bash comparison operator always true
(1 answer)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
Closed 2 years ago.
i am trying to execute a IF condition inside a while loop, But the IF condition isn't working as variables aren't expanding! kindly guide me through the proper way to compare two variables in IF condition
Note - if you can see the error log - DATE was expanding thou! problem with mdate
DATE=`date +"%Y-%m-%d"`
cat path/temp_b | while read file
do
echo 'phase2'
mtime=$(stat -c '%y' $Src_Dir/$file)
echo $mtime
mdate= echo $mtime | cut -d ' ' -f1
echo $mdate
echo $DATE
if ["$mdate"=="$DATE"]; then
"$file" > path/tempc
else
echo 'hi'
fi
done
**Error log -
phase2
2020-05-07 05:22:28.000000000 -0400
2020-05-07
2020-07-21
./test1.ksh: line 37: [==2020-07-21]: command not found
hi**
Change your if statement like:
if [ "$mdate" == "$DATE" ]; then
Explanation: if in bash needs to have square brackets, operator, and operand to be space-separated.

Why two numbers not summarized in script? [duplicate]

This question already has answers here:
How do I echo a sum of a variable and a number?
(9 answers)
How can I do basic maths in bash?
(4 answers)
How do I print the result of a command with 'echo'?
(1 answer)
Closed 4 years ago.
I am newer in bash.
I try to create script that summarizetwo numbers:
Here is script:
echo "the result is:" expr $1+$2
Here how I call the script:
./scr3 50 98
And here is result that I get after the script executed:
the result is: expr 50+98
While I get the the string with two summarized numbers I expect to get the summarize of two numbers.
My question is why I don't get the result of summarize of the two numbers?
Why? Because echo prints its arguments, and you're passing expr as an argument.
A best-practice alternative would be:
echo "The result is: $(( $1 + $2 ))"
...though the a smaller change (albeit to very inefficient code; expr is an artifact of the 1970s, made irrelevant with the introduction of $(( )) in the 1992 POSIX sh standard, and should never be used in new development) is simply:
echo "The result is: $( expr "$1" + "$2" )"

Unexpected end of file bash [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 5 years ago.
lines=`grep -c "" List`
a=1
while [$a -lt $lines]
do
b=`sed "${a} q;d" List`
a=$a+1
echo $b
done
So, I have this, what is supposed to take a line from a list and echo it.
Now, the while loop is not working(?) and returns me this error:HARR.sh: line 9: syntax error: unexpected end of file
What is wrong with it?
Is this what you want?
#!/bin/bash
#
let a=1
while read line; do
echo "$a $line"
let a+=1
done < List

Shell script "current_id: command not found" error [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
When I run my script:
#!/usr/bin/env bash
read NUM
case $NUM in
1)
current_id = "$$"
ps -ef > file1.txt
echo "$current_id"
while [ $current_id -ne 1 ]
do
current_id =$( cat file1.txt | awk '(if ( $s == '$current_id' ) print $3;)')
echo " | "
echo " v "
echo $current_id
done
echo "";;
I get the error:
current_id: command not found
[: -ne: unary operator expected
I am trying to find the child-parent tree with this method. Is there something wrong with my syntax? Or is the current_id = "$$" not allowed? Thank for your help.
In assignment, there must not be a space before =
current_id =$(...) # tries to run a program called current_id, which does not exist
current_id=$(...) # assigns a value to variable

Resources