LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 01-23-2004, 12:49 PM   #1
ky-lab_rat
Member
 
Registered: Jan 2004
Posts: 44

Rep: Reputation: 15
parse output


Ok I have a newbie Q.. I script heavily in the Windows World (I know I'm a trader but, I'm starting to love the power of the dark-side) and trying to learn bash scripting. What I'm trying to do is create a script that will read the output from ls and set a variable for each element it returns. I am looking more so for the extension of the file that is returned. For example in the windows world it would be



for /f "tokens=2 delims=." %%i in ('dir c:\home\fred.* /b') do set ext=%%i


The main thing I'm looking for is in Linux, read the contents of a directory for a file that starts with a certain name. Then sets the extension of that file as a variable so it can be used later on in the script.

This is what I have so far.

=====================================
#!/bin/sh
if test -z $2 ; then
echo Need input
echo $0 vm_to_move target_server
exit $?
fi

ls -al /home/vmware/vmware/$1/$1.vmx |grep -i "vmx" >/dev/null #this is where I would use the windows command from above

if [ $? = 0 ]
then
ext=vmx
elif [ $? = 1 ]
then
ext=cfg

fi


#makes dir on remote host
echo making /home/vmware/vmware/$1 on $2
ssh vmware@$2 mkdir /home/vmware/vmware/$1

#copying home files
echo copying config files from /home/vmware/vmware/$1 to $2
scp /home/vmware/vmware/$1/* vmware@$2:/home/vmware/vmware/$1

#register vm on new host
echo registering $1 on $2
ssh vmware@$2 vmware-cmd -s register /home/vmware/vmware/$1/$1.$ext

#copying image file
echo copying /vmfs/vol1/$1 to $2:/vmfs/vol1
scp /vmfs/vol1/$1* vmware@$2:/vmfs/vol1

==============================================

I am looking for a way to read the extension instead of having it hard coded.

Any and all help with this would be greatly appreciated! THANKS!!!!!!
 
Old 01-23-2004, 01:15 PM   #2
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, and welcome to LQ!

What does %%i do in the windows script?

I can't fully understand what exactly you're
after ...

But if you want to do a grep and be able to use
the bit after the period you may want to have a
closer look at sed or awk...


Cheers,
Tink
 
Old 01-23-2004, 01:21 PM   #3
Nis
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: Ubuntu Hoary (5.04)
Posts: 550

Rep: Reputation: 31
Try 'echo $filename | rev | cut -d . -f 1 | rev'
rev reverses whatever it's given and cut will search up to the first . and return everything before that.
 
Old 01-23-2004, 01:22 PM   #4
ky-lab_rat
Member
 
Registered: Jan 2004
Posts: 44

Original Poster
Rep: Reputation: 15
Whenever you use for in a bath file you have to use 2 %% before the variable.

Excerpt: (from for /?)
To use the FOR command in a batch program, specify %%variable instead
of %variable.
 
Old 01-23-2004, 01:29 PM   #5
ky-lab_rat
Member
 
Registered: Jan 2004
Posts: 44

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by Nis
Try 'echo $filename | rev | cut -d . -f 1 | rev'
rev reverses whatever it's given and cut will search up to the first . and return everything before that.
Thanks that give me the extension but how do I set a variable from that output?
 
Old 01-23-2004, 01:34 PM   #6
Nis
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: Ubuntu Hoary (5.04)
Posts: 550

Rep: Reputation: 31
variable_name=`echo $filename | rev | cut -d . -f 1 | rev`
Notice before echo and after rev are `(backtick) not '(single quote). Very important. Backticks tell the shell to execute the command before assignment. ` can be found right next to '1' on the keyboard.
 
Old 01-23-2004, 01:40 PM   #7
ky-lab_rat
Member
 
Registered: Jan 2004
Posts: 44

Original Poster
Rep: Reputation: 15
Sweet!!! Thank you very much sir!
 
Old 01-23-2004, 01:47 PM   #8
Nis
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: Ubuntu Hoary (5.04)
Posts: 550

Rep: Reputation: 31
You're welcome.
 
  


Reply



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
Parse Error, but why? Mistro116@yahoo.com Programming 2 11-13-2005 02:38 PM
parse error with bc fisayo Programming 7 08-04-2005 09:23 AM
how to pipe and parse output of a command learnfast Linux - Newbie 2 06-15-2005 04:55 AM
Via AC'97 5.1 Optical Output or Audigy 4.1 Output Nza Fedora 3 06-01-2004 07:49 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM

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

All times are GMT -5. The time now is 02:48 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