LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-24-2011, 08:10 PM   #1
hankta
LQ Newbie
 
Registered: Aug 2011
Posts: 2

Rep: Reputation: Disabled
Execute shell command in a variable


I am trying to write a shell script to grab the filename in a directory with that has a specific pattern but having problem when piping grep.

For example:
Patvar=`date +"%Y%m%d" # pattern for grep to search
Fname=`ls /home/abc/ | grep ${Patvar}`
echo " the file is: $Fname"

it returned an empty string!!!
if I used I enter a fix string it will work but not as a variable!

What did I do wrong?
 
Old 08-24-2011, 08:33 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You missed the final backquote there on the first line.
` cmd ...`

You might find it easier to use the alternate version $(cmd ...)
Code:
Patvar=$(date +"%Y%m%d") # pattern for grep to search
Fname=$(ls /home/abc/ | grep $Patvar )
echo " the file is: $Fname"
The {} in the 2nd line were redundant in this instance.
 
1 members found this post helpful.
Old 08-24-2011, 09:20 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi, welcome to LQ!

If this is your actual script you're missing a closing back-tick on the
first line. If it's not there's something else not quite right...
Code:
Patvar=$(date +"%Y%m%d")
touch test$(date +"%Y%m%d").txt
Fname=$(ls  | grep ${Patvar})
echo ${Fname}
test20110825.txt
This behaves the same w/ back-ticks.

Cheers,
Tink


P.S.: Darn, I was AFK for too long on that one ;D

Last edited by Tinkster; 08-24-2011 at 09:21 PM. Reason: PS
 
1 members found this post helpful.
Old 08-25-2011, 12:10 AM   #4
hankta
LQ Newbie
 
Registered: Aug 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you - it is working now.
 
Old 08-25-2011, 01:06 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
In general, variables are for holding data, not code. That's what functions are for. See here:

http://mywiki.wooledge.org/BashFAQ/050

Also, you should never parse ls for file names. Use find, or globbing, or some other technique.

http://mywiki.wooledge.org/ParsingLs

So a more proper solution would be something like this:

Code:
findbydate() {
	local searchvar=$( date +"%Y%m%d" )
	echo -n /home/abc/*$searchvar*

	# or with find
	#find /home/abc -maxdepth 1 -name "*$searchvar*" -print
}

Fname="$( findbydate )"
echo " the file is: $Fname"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I execute a shell command and put the result in command field? fran4tw Linux - General 9 08-08-2011 08:27 PM
Execute command with spaces from variable in bash script klo_2k Linux - Newbie 4 04-13-2008 02:59 AM
Perl:Not able to execute command in variable jags.singh Programming 1 07-23-2007 07:51 AM
PHP can't execute shell command ! rome Programming 3 11-15-2005 12:46 PM
cannot execute 'cd /' shell command from PL/SQL.... samyakm Linux - Newbie 1 01-03-2005 10:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration