I am using below command to find a most recent file with name "candump"
ls *.log | grep "candump" | tail -n 1
The output is "candump-2018-04-19_131908.log"
I want to store the output filename to a variable in my shell script. I am using the below commands:
logfile = `ls *.log | grep "candump" | tail -n 1`
and
logfile = $(ls *.log | grep "candump" | tail -n 1)
However, both times I am getting the same error, "logfile: command not found". Am I doing something wrong? Any help is appreciated.
You have to stick the variable name and its value (no space before and after the =).
Try :
logfile=$(ls *.log | grep "candump" | tail -n 1)
This is working for me.
#!/bin/bash
my_command='ls | grep server.js | wc -l';
my_data=$(eval "$my_command");
echo "value in echo is:" $my_data;
if [ $my_data == "1" ]; then
echo "value is equal to 1";
else
echo "value is not equal to 1";
fi
Related
I'm trying to use the result of a pipe into a "var" and then use it on a echo like this way:
ls -al --time-style=+%D |
grep `date +%D` |
wc -l |
HERE I DON'T KNOW HOW TO ASSING THE VALUE OF THE PREVIOUS PIPE INTO var |
echo "the number is %var"
The idea is to print "the number is 3" (because ls -al --time-style=+%D | date +%D | wc -l is 3)
How can I do it?
One way, using command substitution:
$ var=$(ls -al --time-style=+%D | grep `date +%D` | wc -l)
$ echo "the number is $var"
the number is 3
$
In this if condition for shell script, i want print user full name + exist(if the file ..../pub_key.asc found)or not exist if the mentioned file not found.
for file in $FILES
do
if [ -f /home/$file/public_html/pub_key.asc ]; then
echo $(cat /home/$file/etc/passwd | head -n | cut -d: -f1) : Exists
else
echo $(cat /home/$file/etc/passwd | head -n | cut -d: -f1) : Not exists!
fi
done
But i get errors because there is something wrong with first echo in if..condition.
Can someone show tell me what i did worng in the echo statement? and how to fix? appreciate your suggestions. :)
You are probably getting an error due to head -n command
head -n has to be followed by a number, e.g head -n 1, or head -1
I would like to make array which put users in a time using for loop. For example:
y[1]="user1"
y[2]="user2"
...
y[n]="usern"
I tried to do it like this
#!/bin/bash
x=$(who | cut -d " " -f1 | sort | uniq | wc -l)
for (( i=1; i<=$x; i++ )); do
y[$i]=$(who | cut -d " " -f1 | sort | uniq | sed -n '$ip')
p[$i]=$(lsof -u ${y[$i]} | wc -l)
echo "Users:"
echo ${y[$i]}
echo -e "Number of launched files:\n" ${p[$i]}
done
Most likely I'm using command "sed" wrong.
Can you help me?
Indeed your sed command seems to be a bit off. I can't really guess what you're trying to do there. Besides that, I'm wondering why you're executing who twice. You can make use of the data first obtained in the following manner.
#!/bin/bash
# define two arrays
y=()
p=()
#x=0
while read -r username; do
y+=("$username")
p+=($(lsof -u $(id -u "$username") | wc -l))
echo -e "User:\n${y[-1]}"
echo -e "Open files:\n${p[-1]}"
# The -1 index is the last index in the array, but you
# could uncomment the x=0 variable and the line below:
#((x++))
done <<< $(who | cut -d " " -f1 | sort | uniq)
echo "Amount of users: $x"
exit 0
OS:Yocto
I want to assign the shell output to a variable,
but get the error "test.sh: line 3: out: command not found".
How to do that ...?
this is my code:
#!/bin/bash
out = "$(ls /dev/ | grep "tty" | wc -l)"
echo"$out"
I tried this:
How to set a variable to the output from a command in Bash?
Whitespace matters.
#!/bin/bash
out="$(ls /dev/ | grep "tty" | wc -l)"
echo "$out"
when you assign value to variable don't keep Whitespace before and after "=" that makes error in bash
#!/bin/bash
out="$(ls /dev/ | grep "tty" | wc -l)"
echo"$out"
Try strip spaces around =, i.e. out="$(ls /dev/ | grep "tty" | wc -l)"
I'm at the following directory...
/var/log/homes
and I would like a script to check for any lines starts with 'Error' and contains a word 'NotAuthorized' and not contains '13024' or '31071'.
Within /var/log/homes/, there are 700 files, but that doesn't matter...
Below is what I have...
#!/bin/bash
host=`hostname`
date=`date`
monitor='Error'
pattern='Error'
pattern2='NotAuthorized'
ignore=13024
ignore2=31071
logfile='/var/log/homes/*'
mailto='test#linux.com'
if [ -e $logfile ]
then
if [ `grep -i "$pattern" "$pattern2" "$logfile" | grep -v $ignore $ignore ]
then
echo "Errors found in $monitor at $date - see log $logfile on server $host for full details" |mail -s "ALERT - $errors in logs, please review" $mailto
elif [ `grep -i "$pattern2" "$logfile" |wc -l` -lt 1 ]
fi
else
echo "Logfile $logfile doesn't exist on server $host at $date, this is probably bad, please investigate" |mail -s "ALERT - $monitor monitor has an issue" $mailto
fi
This should work:
grep "^Error" $logfile | grep "NotAuthorized" | grep -v "13024" | grep -v "31071"
grep "^Error": get the lines starting ith "Error"
grep "NotAuthorized": get the lines containing "NotAuthorized"
grep -v "XXX": get the lines not containing "XXX"
It looks fine to me. Just cross check if you are using all of these conditions:
grep "^Error" $file | grep "NotAuthorized" | grep -Ev "31071|13024"
^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
lines starting with Error | |
containing NotAuthorized
exclude either lines containing 31071 or 13024
To know which file is it happening in, use -H option in grep:
grep -H "^Error" $file | grep "NotAuthorized" | grep -Ev "31071|13024"
So all together you can do:
grep -H "^Error" /var/log/homes/* | grep "NotAuthorized" | grep -Ev "31071|13024"
Print any lines that start with 'Error' and contain the word 'NotAuthorized' and does not contain '13024' or '31071':
sed -n '/^Error/{/NotAuthorized/{/13024/!{/31071/!p}}}'
This awk may do:
awk '/^Error/ && /NotAuthorized/ && !/31071|13024/' $logfile