operand expected (Error token is "-") and ambiguous redirect - bash

I have to errors, operand expected and ambiguous redirect.
Here's my code:
#!/bin/bash
read input >| inputfile
file_name=$(cut -d" " -f1 inputfile)
i=$(cut -d" " -f2 inputfile)
j=$(cut -d" " -f3 inputfile)
k=$(cut -d" " -f4 inputfile)
l=$(cut -d" " -f5 inputfile)
maxlinetoget=$[$l-$k]
currentlinecount=1
result=0
while read line
do
if [ $currentlinecount -ge $k && $currentlinecount -le $l ]
then
echo -n $line >| linefile
echo -n $line
for number in linefile
do
echo $number
result=$[$result+$number]
done
fi
currentlinecount=$[$currentlinecount+1]
done < $file_name
echo $result
And the errors:
./P4.4: line 8: -: syntax error: operand expected (error token is "-")
./P4.4: line 24: $file_name: ambiguous redirect
line 8 is: maxlinetoget=$[$l-$k]
line 24 is: done < $file_name
I have no idea what's wrong, please tell me.
Thanks.

Both errors are the result of the fact that neither $l not $file_name have a value. Bash sometimes produces mysterious error messages when unquoted variables are empty. (You would have gotten more sensible error messages if you'd used $((l-k)) instead of the deprecated $[$l-$k], or had quoted your substitutions, particularly "$file_name".)
read line >| inputfile
reads one line from stdin and puts it in the variable $line. It produces no output, so inputfile is empty. Consequently, all of the following cut commands produce no output (nothing-in, nothing-out: the infamous NINO).
What you apparently wanted to do was
read file_name i j k l rest
((maxlinetoget = l - k))
# Or maxlinetoget=$((l-k))

Related

Concatenate two strings to form a shell command results in a strange behaviour

I have this code:
#!/bin/ksh
value=''
builddir=`dirname $0`
cd $builddir
while read line
do
echo line: $line
param=`echo $line|cut -d '=' -f1`
if [[ $param = 'profile.home' ]]
then
value=`echo $line|cut -d '=' -f2`
echo was_profile:$value
break
fi
done < was-config.properties
if [[ $value = '' ]]
then
echo "Please configure the profile.home dir into the was.config.properties file and run again the script"
else
"$value"/bin/ws_ant.sh -buildfile $builddir/build.xml $#
fi
Which returns:
line: #was configuration properties
line:
line: was.home=/opt/IBM/WebSphere/AppServer/
line: profile.home=/opt/IBM/WebSphere/AppServer/profiles/AppSrv01
was_profile:/opt/IBM/WebSphere/AppServer/profiles/AppSrv01
/bin/ws_ant.sh: not foundBM/WebSphere/AppServer/profiles/AppSrv01
why do I receive a not found? It seems the concatenation "$value"/bin/ws_ant.sh
is not working!!!
There are carriage returns (0A or ^M or \r) in your file, see How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script?.
In your case you do not need to loop through the config file. You can use
value=$(grep profile.home= was-config.properties | cut -d= -f2 | tr -d '\r')
or
value=$(sed -rn 's/^profile.home=([^\r]*)*/\1/p' was-config.properties)
or
value=$(awk -F "\r|=" '/^profile.home/ {print $2}' was-config.properties)

How to cut variables which are beteween quotes from a string

I had problem with cut variables from string in " quotes. I have some scripts to write for my sys classes, I had a problem with a script in which I had to read input from the user in the form of (a="var1", b="var2")
I tried the code below
#!/bin/bash
read input
a=$($input | cut -d '"' -f3)
echo $a
it returns me a error "not found a command" on line 3 I tried to double brackets like
a=$(($input | cut -d '"' -f3)
but it's still wrong.
In a comment the OP gave a working answer (should post it as an answer):
#!/bin/bash
read input
a=$(echo $input | cut -d '"' -f2)
b=$(echo $input | cut -d '"' -f4)
echo sum: $(( a + b))
echo difference: $(( a - b))
This will work for user input that is exactly like a="8", b="5".
Never trust input.
You might want to add the check
if [[ ${input} =~ ^[a-z]+=\"[0-9]+\",\ [a-z]+=\"[0-9]+\"$ ]]; then
echo "Use your code"
else
echo "Incorrect input"
fi
And when you add a check, you might want to execute the input (after replacing the comma with a semicolon).
input='testa="8", testb="5"'
if [[ ${input} =~ ^[a-z]+=\"[0-9]+\",\ [a-z]+=\"[0-9]+\"$ ]];
then
eval $(tr "," ";" <<< ${input})
set | grep -E "^test[ab]="
else
echo no
fi
EDIT:
#PesaThe commented correctly about BASH_REMATCH:
When you use bash and a test on the input you can use
if [[ ${input} =~ ^[a-z]+=\"([0-9]+)\",\ [a-z]+=\"([0-9])+\"$ ]];
then
a="${BASH_REMATCH[1]}"
b="${BASH_REMATCH[2]}"
fi
To extract the digit 1 from a string "var1" you would use a Bash substring replacement most likely:
$ s="var1"
$ echo "${s//[^0-9]/}"
1
Or,
$ a="${s//[^0-9]/}"
$ echo "$a"
1
This works by replacing any non digits in a string with nothing. Which works in your example with a single number field in the string but may not be what you need if you have multiple number fields:
$ s2="1 and a 2 and 3"
$ echo "${s2//[^0-9]/}"
123
In this case, you would use sed or grep awk or a Bash regex to capture the individual number fields and keep them distinct:
$ echo "$s2" | grep -o -E '[[:digit:]]+'
1
2
3

How to redirect grep to a while loop

Hi I have the following bash script code
group2=0
while read -r line
do
popAll=$line | cut -d "{" -f2 | cut -d "}" -f1 | tr -cd "," | wc -c
if [[ $popAll = 0 ]]; then
group2 = $((group2+2));
else
group2 = $((group2+popAll+1));
fi
done << (grep -w "token" "$file")
and I get the following error:
./parsingTrace: line 153: syntax error near unexpected token `('
./parsingTrace: line 153: `done << (grep -w "pop" "$file")'
I do not want to pipe grep to the while, because I want variable inside the loop to be visible outside
The problem is in this line:
done << (grep -w "token" "$file")
# ^^
You need to say < and then <(). The first one is to indicate the input for the while loop and the second one for the process substitution:
done < <(grep -w "token" "$file")
# ^ ^
Note however that there are many others things you want to check. See the comments for a discussion and paste the code in ShellCheck for more details. Also, by indicating some sample input and desired output I am sure we can find a better way to do this.

Errors in bash script. Syntax error near unexpected token

Do you know what is wrong with my script as I always get the error mesage:
position frac1 frac2
: command not found:
'/s1_met.sh: line 3: syntax error near unexpected token `do
'/s1_met.sh: line 3: `for lineF1 in $(cat $1); do
Code here:
export IFS=$'\n'
echo "position frac1 frac2";
for lineF1 in $(cat $1); do
if [ $(echo $lineF1 | cut -b 1-2) = "##" ]; then
echo "skip line" >&2;
else
startF1=$(echo $lineF1 | cut -f 4);
stopF1=$(echo $lineF1 | cut -f 5);
fracF1=$(echo $lineF1 | cut -f 9 | cut -d ";" -f 4 | cut -d "=" -f 2);
lineF2=$(grep "$startF1" $2);
if [ -z "$lineF2" ]; then
echo "position $startF1 cannot be found" >&2;
else
fracF2=$(echo $lineF2 | cut -f 9 | cut -d ";" -f 4 | cut -d "=" -f 2);
echo "$startF1 $fracF1 $fracF2";
fi;
fi
done;
There's nothing wrong with it, you must not be running it with BASH.
Edited to say you need to check your line endings, your comment below with the ^M means that you have extra characters on the line. See here.
https://stackoverflow.com/tags/bash/info
Try putting the "shebang" line in the script shebang docs
To do this, run which bash which will tell you something like /bin/bash. Your script should then be:
#!/bin/bash
echo "I'm running with bash!"
Try that, your syntax is OK.

How to get output of grep in single line in shell script?

Here is a script which reads words from the file replaced.txt and displays the output each word in each line, But I want to display all the outputs in a single line.
#!/bin/sh
echo
echo "Enter the word to be translated"
read a
IFS=" " # Set the field separator
set $a # Breaks the string into $1, $2, ...
for a # a for loop by default loop through $1, $2, ...
do
{
b= grep "$a" replaced.txt | cut -f 2 -d" "
}
done
Content of "replaced.txt" file is given below:
hllo HELLO
m AM
rshbh RISHABH
jn JAIN
hw HOW
ws WAS
ur YOUR
dy DAY
This question can't be appropriate to what I asked, I just need the help to put output of the script in a single line.
Your entire script can be replaced by:
#!/bin/bash
echo
read -r -p "Enter the words to be translated: " a
echo $(printf "%s\n" $a | grep -Ff - replaced.txt | cut -f 2 -d ' ')
No need for a loop.
The echo with an unquoted argument removes embedded newlines and replaces each sequence of multiple spaces and/or tabs with one space.
One hackish-but-simple way to remove trailing newlines from the output of a command is to wrap it in printf %s "$(...) ". That is, you can change this:
b= grep "$a" replaced.txt | cut -f 2 -d" "
to this:
printf %s "$(grep "$a" replaced.txt | cut -f 2 -d" ") "
and add an echo command after the loop completes.
The $(...) notation sets up a "command substitution": the command grep "$a" replaced.txt | cut -f 2 -d" " is run in a subshell, and its output, minus any trailing newlines, is substituted into the argument-list. So, for example, if the command outputs DAY, then the above is equivalent to this:
printf %s "DAY "
(The printf %s ... notation is equivalent to echo -n ... — it outputs a string without adding a trailing newline — except that its behavior is more portably consistent, and it won't misbehave if the string you want to print happens to start with -n or -e or whatnot.)
You can also use
awk 'BEGIN { OFS=": "; ORS=" "; } NF >= 2 { print $2; }'
in a pipe after the cut.

Resources