What's the Error? - shell

I'm unable to detect the cause of error.
Please help point me out and it's corrective action. I'm a beginner and so this will be a great help to me.
mapping.txt:
test1 hello
test2 world
My Sh file:
parameter 1 = hello
a= cat mapping.txt | grep $1 | awk '{print$1}'
echo $a
## Extracting Dump name
b=$(ls -ltr /home/oracle/$a/$1*.dmp | awk '{print $9}' | tail -1)
I'm getting test1 as echo, but i'm unable to substitute in /home/oracle/$a/$1*.dmp
script
output: ls: /home/oracle//hello*.dmp: No such file or directory
What change should i do to let it substitute as: /home/oracle/test1/hello*.dmp

From the error, you can see the variable a is not set:
Change your first line from:
a= cat mapping.txt | grep $1 | awk '{print$1}'
to:
a=$(cat mapping.txt | grep $1 | awk '{print$1}')

Related

Shell Script Segfault

I've been trying to run the following function in a .sh file.
function run_test()
{
local executable=$1
BINNAME=$(basename ${executable})
local LOGDIR=$2
# Get the list of timestamps from the log file names in log directory 'LOGDIR'
LIST_OF_TS=`ls $LOGDIR/*.gz | awk -F "/" '{print $NF}' | awk -F'_' '{print $2}' | awk -F'.'
echo "Adding run of $executable to $RUNDIR/$BINNAME.dat"
for i in $LIST_OF_TS; do
gunzip -c $LOGDIR/*$i*.gz | sort -n
done | $executable "$2" > $RUNDIR/$BINNAME.dat
echo "Finished $executable"
}
This gives the error
for i in $LIST_OF_TS;
do
gunzip -c $LOGDIR/*$i*.gz | sort -n;
done
11726 Segmentation fault (core dumped) | $executable "$2" > $RUNDIR/$BINNAME.dat
When I remove the "$2", this works fine. My goal is to pass in $2 (or $LOGDIR) as an argument to $executable. I have no idea why this segfaults. Any advice?

unexpected EOF while looking for matching error

I am running below script and getting
error script.sh: line 9: unexpected EOF while looking for matching `''
script.sh: line 15: syntax error: unexpected end of file.
Though I tried to run line 9 manually n it runs without error.
alias gxt="awk -F "_" '{print \$1}' test | uniq"
count = $(cat test | awk -F "_" '{print $1} | uniq | wc -l)
for i in {1..count};
do
User=$(gxt | head -n $i)
recharge=$(grep -E "$User.recharge" test| awk -F "_" '{print $3}' | xargs )
total1=( $((${recharge// /+})))
sales=$(grep -E "$User.sale" test| awk -F "_" '{print $3}' | xargs )
total2=( $((${sales// /+})))
balance=`expr $total1 - $total2`
echo $User.balance.$balance >> result
done
Other than the issues already reported and those exposed by shellcheck, there is another issue:
for i in {1..count};
'count' cannot be a variable. It can only be a constant.
Change it to
for ((i = 1; i <= count; i++)); do whatever ; done
count=$(cat test | awk -F "_" '{print $1}` | uniq | wc -l)
missing ' at the end of {print $1}
Inadvertently added spaces around =

Bash for loop error

I have a script in bash what take from LocLog a ip from collumn 8 :
#!/bin/bash
for i in $(cat /scripts/logs/LocLog | awk '{print $8}' | sort | uniq);
do
php /scripts/a.php $i;
done
The script give an error:
bash -x log
'og: line 2: syntax error near unexpected token `
'og: line 2: `for i in $(cat /scripts/logs/LocLog | awk '{print $8}' | sort | uniq);
Any ideeas?
Get rid of the semicolon at the end of the for line.

Use each line of piped output as parameter for script

I have an application (myapp) that gives me a multiline output
result:
abc|myparam1|def
ghi|myparam2|jkl
mno|myparam3|pqr
stu|myparam4|vwx
With grep and sed I can get my parameters as below
myapp | grep '|' | sed -e 's/^[^|]*//' | sed -e 's/|.*//'
But then want these myparamx values as paramaters of a script to be executed for each parameter.
myscript.sh myparam1
myscript.sh myparam2
etc.
Any help greatly appreciated
Please see xargs. For example:
myapp | grep '|' | sed -e 's/^[^|]*//' | sed -e 's/|.*//' | xargs -n 1 myscript.sh
May be this can help -
myapp | awk -F"|" '{ print $2 }' | while read -r line; do /path/to/script/ "$line"; done
I like the xargs -n 1 solution from Dark Falcon, and while read is a classical tool for such kind of things, but just for completeness:
myapp | awk -F'|' '{print "myscript.sh", $2}' | bash
As a side note, speaking about extraction of 2nd field, you could use cut:
myapp | cut -d'|' -f 1 # -f 1 => second field, starting from 0

Why I can't split the string?

I want to read a file by shell script, and process it line by line. I would like to extract 2 fields from each line. Here is my code:
#!/bin/bsh
mlist=`ls *.log.2011-11-1* | grep -v error`
for log in $mlist
do
while read line
do
echo ${line} | awk -F"/" '{print $4}' #This produce nothing
echo ${line} #This work and print each line
done < $log | grep "java.lang.Exception"
done
This is a sample line from the input file:
<ERROR> LimitFilter.WebContainer : 4 11-14-2011 21:56:55 - java.lang.Exception: File - /AAA/BBB/CCC/DDDDDDDD.PDF does not exist
If I don't use bsh, I can use ksh, and the result is the same. We have no bash here.
It's because you are passing the output of your while loop through grep "java.lang.Exception".
The output of echo $line | awk -F"/" '{print $4}' is CCC. When this is piped through grep, nothing is printed because CCC does not match the search pattern.
Try removing | grep "java.lang.Exception" and you will see the output of your loop come out correctly.
An alternative approach to take might be to remove the while loop and instead just use:
grep "java.lang.Exception" $log | awk -F"/" '{print $4}'

Resources