3

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.

1

2 Answers 2

15

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, was a silly mistake on my part.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.