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

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

Related

search for a variable in a string in bash [duplicate]

This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 6 months ago.
I have a string given below:
string1 = "Hello there, my name is Jack.
Hello there, my name is Jack.
Hello there, my name is Jack."
I'm taking the following input from the string:
read string2
I want to check whether the string2(which is a variable) is present in string1.
I tried running the below command:
output=$(echo $string1 | grep -o "$string2")
echo $output
eg: Let string2="name"
The output is empty when I'm running this command.
Can someone tell me where am I going wrong?
#!/bin/bash
string1="Hello there, my name is Jack"
string2="name"
if [[ $string1 == *"$string2"* ]]; then
echo "$string2 found"
else
echo "$string2 not found"
fi
Alternate method with POSIX-shell grammar:
string1='Hello there, my name is Jack'
string2='name'
case "$string1" in
*"$string2"*) printf '%s found\n' "$string2";;
*) printf '%s not found\n' "$string2";;
esac

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

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?

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.

setting variables with variable names in bash (para$i=... giving "command not found") [duplicate]

This question already has answers here:
Assign variables inside for loops
(2 answers)
Closed 6 years ago.
I want to pass three arguments to a script,the first two numbers and third one any character,Buut when i run the script it says command not found ,even though the value is getting assigned.i have attached the code and image below.enter image description here
This is my peice of code,
#!/bin/bash
if [ $# -lt 3 ]
then
echo "insufficient argument"
for((i=$#+1;i<4;i=$i+1))
do
read -p "enter $i parameter: " x
para$i=x
done
fi
This is not a valid assignment:
para$i=x
Since your shell is bash, you can do the following instead:
# bash 3.1 or higher
printf -v "para$i" %s "$x"
...or...
# bash 4.3 or higher; works with arrays and other tricky cases too.
declare -n para="para$i"
para=$x
unset -n para
...or...
# any POSIX shell
# be very careful about the quoting; only safe if $x is quoted and $i is a controlled value
eval "para$i=\$x"
See the BashFAQ #6 section on indirect assignment for more details.
See an example of processing a parameter called process_date
The script this will accept the parameter as so:
some_sh_script.sh -process_date=01/01/2016
Script:
process_date=""
while test "$1" != "" ; do
# Test argument syntax e.g. -someName=someValue or help operators
if [[ $1 != -*=* && $1 != -h && $1 != -help ]]
then
echo "Error in $0 - $1 - Argument syntax invalid."
usage
exit 1
fi
# END Test argument syntax
# Split argument name & value by `=` delimiter
paramName=`echo $1 | cut -d '=' -f1`
paramVal=`echo $1 | cut -d '=' -f2`
case $paramName in
-process_date)
process_date=$paramVal
;;
#User help parameter
-help|-h)
usage
exit 0
;;
-*)
echo "No such option $1"
usage
exit 1
;;
esac
#parse next argument
shift
done

What does the colon dash ":-" mean in bash [duplicate]

This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 8 years ago.
The result is the one desired; after a bit of trial and error. I don't understand what the "2:-" and "3:-" do/mean. Can someone explain.
#!/bin/bash
pid=$(ps -ef | grep java | awk ' NR ==1 {print $2}')
count=${2:-30} # defaults to 30 times
delay=${3:-10} # defaults to 10 second
mkdir $(date +"%y%m%d")
folder=$(date +"%y%m%d")
while [ $count -gt 0 ]
do
jstack $pid >./"$folder"/jstack.$(date +%H%M%S.%N)
sleep $delay
let count--
echo -n "."
done
It's a parameter expansion, it means if the third argument is null or unset, replace it with what's after :-
$ x=
$ echo ${x:-1}
1
$ echo $x
$
There's also another similar PE that assign the value if the variable is null:
$ x=
$ echo ${x:=1}
1
$ echo $x
1
Check http://wiki.bash-hackers.org/syntax/pe

Resources