Why is this giving me an error ? count is - shell

ls>out
count="$(ls|wc -l)"
for i in {1..$count-2}
do
if[ "$(cat out|head -$count | tail -1)" != "rename.sh" ]; then
mv "$1$count$2" | cat out | head -$count | tail -1
fi
done
I really don't get why is not working...

ls>out
count="$(ls|wc -l)"
for i in {1..$count-2}
do
if [ "$(cat out|head -$count | tail -1)" != "rename.sh" ]; then
mv "$1$count$2" | cat out | head -$count | tail -1
fi
done
put a space between if and '[' , the script will be work.

Related

How to fix an unexpected end of file error in bash?

The code saying that there is "unexpected end of file" error in line 16. Could someone please tell me my mistake?
#!/bin/bash
total=0
for i in `grep 01/Oct/2006 log.txt | cut -d' ' -f1 | sort | uniq -c | sort -n | tail`;
do if [[ $i =~ ^[0-9]+$ ]]; then
total=$(( $total + $i )); fi
for i in `grep 01/Oct/2006 log.txt | cut -d' ' -f1 | sort | uniq -c | sort -rn | head -10 | tr -s ' ' | cut -d' ' -f2,3 | sed -E 's/(\S*) (\S*)/\2 - \1/' | nl -s'. '`;
do
if ! [[ $i =~ ^[0-9]+$ ]];
then
printf $i;
printf " ";
else
printf " $i - $(echo "scale=0; $i * 100 / $total" | bc )%% \n" ;
fi
done
Your first for loop lacks a done.
Here's a working version with improved formatting (but with all original flaws and bugs left inside, I just fixed the one issue asked for here):
#!/bin/bash
$total;
for i in $(
grep 01/Oct/2006 log.txt |
cut -d' ' -f1 |
sort |
uniq -c |
tail);
do
if [[ $i =~ ^[0-9]+$ ]]
then
$total += $i
fi
done
for i in $(
grep 01/Oct/2006 log.txt |
cut -d' ' -f1 |
sort |
uniq -c |
sort -rn |
head -10 |
tr -s ' ' |
cut -d' ' -f2,3 |
sed -E 's/(\S*) (\S*)/\2 - \1/' |
nl -s'. ')
do
if ! [[ $i =~ ^[0-9]+$ ]];
then
printf " $i - ";
else
printf " $i - 0$(echo "scale=0; $i / $total" | bc)%% " ;
fi
done

what is wrong with grep bash function

I have function
function contain() {
if [[ $(echo "$1" | grep "$2" | wc -c) -gt 0 ]]; then
return 0
else
return 1
fi
}
when I write instead
if [[ $(md5sum -c tool | grep -v "OK$" | wc -c) -gt 0 ]]; then
this
if contain "$(md5sum -c tool)" "OK$"
wants not work

BASH: Remove newline for multiple commands

I need some help . I want the result will be
UP:N%:N%
but the current result is
UP:N%
:N%
this is the code.
#!/bin/bash
UP=$(pgrep mysql | wc -l);
if [ "$UP" -ne 1 ];
then
echo -n "DOWN"
else
echo -n "UP:"
fi
df -hl | grep 'sda1' | awk ' {percent+=$5;} END{print percent"%"}'| column -t && echo -n ":"
top -bn2 | grep "Cpu(s)" | \sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \awk 'END{print 100 - $1"%"}'
You can use command substitution in your first sentence (notice you're creating a subshell in this way):
echo -n $(df -hl | grep 'sda1' | awk ' {percent+=$5;} END{print percent"%"}'| column -t ):

Using bash command on a variable that will be used as reference for an array

Short and direct, basically I want to use the value of $command on a variable, instead using it inside the while loop as a command itself. So:
This Works, but I think it's ugly:
#!/bin/bash
IFS=$'\n'
lsof=`which lsof`
whoami=`whoami`
while true ; do
execution_array=($(${lsof} -iTCP -P 2> /dev/null | grep ':' | grep ${whoami} | awk '{print $9}' | cut -f2 -d'>' | sort | uniq ))
for i in ${execution_array[*]}; do
echo $i
done
sleep 1
done
unset IFS
This doesn't work ( no output happens ), but i think is less ugly:
#!/bin/bash
IFS=$'\n'
lsof=`which lsof`
whoami=`whoami`
command="${lsof} -iTCP -P 2> /dev/null | grep ':' | grep ${whoami} | awk '{print $9}' | cut -f2 -d'>' | sort | uniq"
while true ; do
execution_array=($(command))
for i in ${execution_array[*]}; do
echo $i
done
sleep 1
done
unset IFS
This solved my problem:
#!/bin/bash
IFS=$'\n'
lsof=$(which lsof)
list_connections() {
${lsof} -iTCP -P 2> /dev/null | grep ':' | grep $(whoami) | awk '{print $9}' | cut -f2 -d'>' | sort | uniq
}
while true ; do
execution_array=($(list_connections))
for i in ${execution_array[*]}; do
echo $i
done
sleep 1
done
unset IFS

repeat function with different arguments if one variable within the function is empty

The function has to check if TARGET is empty, and if it is empty i want it to be repeated but this time with c1TIME=$hours:$minutes:[0-9][0-9]
then run it, and if it is still empty then with c1TIME=$hours:$c1minutes[0-9]:[0-9][0-9]
after that it should stop and echo some info.
$1 is a standard logfile with errors
$TIME string with time in this pattern HH:MM:SS
I am sorry if this is confusing, I'm appreciate any help
what this function does in detail:
first it takes a random time for example 12:34:56 and reads only
the "5" in c1seconds, then c1TIME is 12:34:5[0-9]
Target now tries to find a line where the string of c1TIME is located in the logfile and
prints the number of the line.
c1DATE changes the 12:34:5[0-9] in the found time like 12:34:56 for example.
the rest is actually unimportant
like I said above when TARGET is empty it has to run again, so how can i check TARGET while or after the function executed?
function check() {
c1seconds=`echo $TIME | awk '{print substr ($1,7,8)}' | head -1c`
c1TIME=$hours:$minutes:$c1seconds[0-9]
TARGET=`cat -n $1 | grep "$c1TIME" | awk '{printf "%s\n",$1}' | awk 'sub("$", "")'| head -1`
c1DATE=`cat $1 | grep "$c1TIME" | grep -Eo "[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}" | head -1`
EXCEPTION=`tail -$[$WINDOW_LINES-$TARGET] $1 | grep -c Exception`
ERROR=`tail -$[$WINDOW_LINES-$TARGET] $1 | grep -c Error`
SEMAPHORE=`tail -$[$WINDOW_LINES-$TARGET] $1| grep -c semaphore`
echo $TARGET
return 0
}
You want to repeat while target is empty: :) so here you go:
function check() {
c1seconds=`echo $TIME | awk '{print substr ($1,7,8)}' | head -1c`
c1TIME=$hours:$minutes:$c1seconds[0-9]
TARGET=`cat -n $1 | grep "$c1TIME" | awk '{printf "%s\n",$1}' | awk 'sub("$", "")'| head -1`
if [ x$TARGET = x ] ; then
c1TIME=$hours:$minutes:[0-9][0-9]
TARGET=`cat -n $1 | grep "$c1TIME" | awk '{printf "%s\n",$1}' | awk 'sub("$", "")'| head -1`
fi
c1DATE=`cat $1 | grep "$c1TIME" | grep -Eo "[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}" | head -1`
EXCEPTION=`tail -$[$WINDOW_LINES-$TARGET] $1 | grep -c Exception`
ERROR=`tail -$[$WINDOW_LINES-$TARGET] $1 | grep -c Error`
SEMAPHORE=`tail -$[$WINDOW_LINES-$TARGET] $1| grep -c semaphore`
echo $TARGET
}
BTW feels like an ugly way to do what you are doing :P
Feels even uglier :) but i don't know what you are trying to accomplish or what your input is so the answer is as it is above.
well you can always use recursion. keep a level=0 at the global level and call the function again.
level=0;
function check() {
Now instead of
c1TIME=$hours:$minutes:$c1seconds[0-9]
use
if [ $level -eq 0 ]; then c1TIME=$hours:$minutes:$c1seconds[0-9]; fi
if [ $level -eq 1 ]; then c1TIME=$hours:$minutes:[0-9][0-9]; fi
if [ $level -eq 2 ]; then c1TIME=$hours:$c1minutes[0-9]:[0-9][0-9]; fi
if [ $level -ge 2 ]; then echo "ERROR"; exit; fi
Now instead of
TARGET=`cat -n $1 | grep "$c1TIME" | awk '{printf "%s\n",$1}' | awk 'sub("$", "")'| head -1`
use
TARGET=`cat -n $1 | grep "$c1TIME" | awk '{printf "%s\n",$1}' | awk 'sub("$", "")'| head -1`
if [ "x$TARGET" = "x" ]; then ((level++)); check $#; fi
I have not run this so typos may be there, but main idea is as the number of recursion and checks are at max 3, use a simple if check with the depth of the recursion. You can modify the series of if to if else. Also if grep can open a file, why cat file and pipe it, and if awk can search for a pattern in a file, why cat file | grep pattern before it?

Resources