Error while running a while loop in Unix - shell

I am executing a script and getting error in the following code. It is working fine on RedHat but in AIX7.1 it is giving errors :
while read line
do
read -A arr <<< $line
ct="$(perl -e 'print time()')"
x=${arr[6]}
y="$(((ct-x)/60/60))"
if [ $y -gt 48 ];then
echo "${arr[0]} ${arr[3]} ${arr[5]} ${arr[6]}" >> $longrunning_jobs_tmp1
fi
done < $active_jobs_tmp4
I have correctly defined the variables also but still getting the following issue :
Job_Monitoring_Test.ksh[121]: 0403-057 Syntax error at line 123 : `<' is not expected.

You are running the script with ksh88 on AIX while it was written for ksh93.
Try setting its shebang to:
#!/bin/ksh93
...
If ksh93 is not available, perhaps dtksh would work. Otherwise, you need to port the script to bash.
Edit:
If the issue is with the <<< syntax, you can replace
read -A arr <<< $line
by:
read -A arr <<%EOF%
$line
%EOF%

Do you have/can you install Bash on your system?
Using the shebang #/bin/bash might solve a lot problems.
You still have to look at the options used in different commands like find (mtime), date and sed (-i option for inplace editing), but Bash specific constructions will work.

Related

This loop will only ever run once. Bad quoting or missing glob/expansion?

Im working on a bash script that recive a list of processes and do a bunch of things with them, however when I want to analyze them with a loop this error happens.
Here is some code:
#! /bin/bash
ls /proc | grep ^9 > processes.txt
cat processes.txt
for line in $processes.txt
do
echo "$line"
done
PD: Im preatty new to bash
$ does parameter expansion; it does not expand a file name to the contents of the file.
Use a while read loop instead.
while IFS= read -r line; do
echo "$line"
done < processes.txt

How to assign variables from the file in a loop

I'm trying to write a bash script that will read pairs of variables from the file and then uses them in a loop. i'm trying to use this
while read p; do $p; echo "$a and $b"; done < text.txt
with the text.txt containing the following:
a="teststring"; b="anothertest"
a="teststring1"; b="anothertest1"
a="teststring2"; b="anothertest2"
the output looks like that:
bash: a="teststring";: command not found
and
bash: a="teststring1";: command not found
and
I have found similar question command line arguments from a file content
But couldn't figure out how to apply the answers to my particular case. Thanks!
One solution using the evil eval, just for tests purpose, not to use in production :
while read line; do
eval "$line"
echo "$a and $b"
done < file.txt
Output:
teststring and anothertest
teststring1 and anothertest1
teststring2 and anothertest2
Please read
http://wiki.bash-hackers.org/commands/builtin/eval

Redirector "<<<" in Ubuntu?

I'm getting this error
Syntax error: redirection unexpected
in the line:
if grep -q "^127.0.0." <<< "$RESULT"
How I can run this in Ubuntu?
<<< is a bash-specific redirection operator (so it's not specific to Ubuntu). The documentation refers to it as a "Here String", a variant of the "Here Document".
3.6.7 Here Strings
A variant of here documents, the format is:
<<< word
The word is expanded and supplied to the command on its
standard input.
A simple example:
$ cat <<< hello
hello
If you're getting an error, it's likely that you're executing the command using a shell other than bash. If you have #!/bin/sh at the top of your script, try changing it to #!/bin/bash.
If you try to use it with /bin/sh, it probably assumes the << refers to a "here document", and then sees an unexpected < after that, resulting in the "Syntax error: redirection unexpected" message that you're seeing.
zsh and ksh also support the <<< syntax.
if grep -q "^127.0.0." <<< "$RESULT"
then
echo IF-THEN
fi
is a Bash-specific thing. If you are using a different bourne-compatable shell, try:
if echo "$RESULT" | grep -q "^127.0.0."
then
echo IF-THEN
fi
It works for me on Ubuntu, if I complete you IF block:
if grep -q "^127.0.0." <<< "$RESULT"; then echo ""; fi

How to use the read command in Bash?

When I try to use the read command in Bash like this:
echo hello | read str
echo $str
Nothing echoed, while I think str should contain the string hello. Can anybody please help me understand this behavior?
The read in your script command is fine. However, you execute it in the pipeline, which means it is in a subshell, therefore, the variables it reads to are not visible in the parent shell. You can either
move the rest of the script in the subshell, too:
echo hello | { read str
echo $str
}
or use command substitution to get the value of the variable out of the subshell
str=$(echo hello)
echo $str
or a slightly more complicated example (Grabbing the 2nd element of ls)
str=$(ls | { read a; read a; echo $a; })
echo $str
Other bash alternatives that do not involve a subshell:
read str <<END # here-doc
hello
END
read str <<< "hello" # here-string
read str < <(echo hello) # process substitution
Typical usage might look like:
i=0
echo -e "hello1\nhello2\nhello3" | while read str ; do
echo "$((++i)): $str"
done
and output
1: hello1
2: hello2
3: hello3
The value disappears since the read command is run in a separate subshell: Bash FAQ 24
To put my two cents here: on KSH, reading as is to a variable will work, because according to the IBM AIX documentation, KSH's read does affects the current shell environment:
The setting of shell variables by the read command affects the current shell execution environment.
This just resulted in me spending a good few minutes figuring out why a one-liner ending with read that I've used a zillion times before on AIX didn't work on Linux... it's because KSH does saves to the current environment and BASH doesn't!
I really only use read with "while" and a do loop:
echo "This is NOT a test." | while read -r a b c theRest; do
echo "$a" "$b" "$theRest"; done
This is a test.
For what it's worth, I have seen the recommendation to always use -r with the read command in bash.
You don't need echo to use read
read -p "Guess a Number" NUMBER
Another alternative altogether is to use the printf function.
printf -v str 'hello'
Moreover, this construct, combined with the use of single quotes where appropriate, helps to avoid the multi-escape problems of subshells and other forms of interpolative quoting.
Do you need the pipe?
echo -ne "$MENU"
read NUMBER

Counting file lines in shell and in a script gives different results

For a bunch of files in a directory I want to get the number of lines for each one, store it
in a variable and do additional stuff. Via shell I can do it without problems if I do
read NLINES <<< $( cat file | wc -l )
but if I do it in a script
#!/bin/bash
for i in `ls *.dat `
do
read NLINES <<< $( cat $i | wc -l )
done
I get
Syntax error: redirection unexpected
Why the difference? How could I fix it?
I bet your default shell isn't bash but something else. Leave the #!/bin/bash and replace it with #!/bin/sh, to let your script use the default shell.
I made this error the other way, when I tried to use some debian scripts on Ubuntu, where #!/bin/sh behaved differently from my assumed #!/bin/bash.

Resources