Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-31-2006, 06:52 PM
|
#1
|
|
Member
Registered: Aug 2004
Location: Argentina
Distribution: slackware 12
Posts: 211
Rep:
|
bash cripting function to "identify" any char in a string?
hi.. I'm traying to make a script that automate this cc command to compile:
cc -o theFile theFile.c
The script should recieve the name of the .c file as it only argument, and creates the binary named as the .c file WHITOUT the .c extension.
To do so, I need to "read" the content of the $1 variable, and be able to "identify" its period and take the first half of the variable's name, and put it on a new variable which will be the name of the binary file.
The fact is I founded how to add a extension to a variable: $1{.bak}
..but not how to take it off from it.
Does anybody know if it is possible using bash scripting?
and if so: which commands should I use to "meassure" and "truncate" a variable?
examples are welcome 
regards,
|
|
|
|
01-31-2006, 07:16 PM
|
#2
|
|
Member
Registered: Sep 2003
Location: Ontario, Canada
Distribution: Gentoo, Suse
Posts: 59
Rep:
|
Well, the easy answer is use "make", ie: "# make MyProg.c" will yield a binary called "MyProg".
If you really wanted to script it:
Code:
dashO = `echo "$1" | sed s/\.c//`
cc $1 -o $dashO
should do it.
HTH
|
|
|
|
01-31-2006, 07:30 PM
|
#3
|
|
Member
Registered: Apr 2003
Location: Midlands, UK
Distribution: Lapwing-Linux 2009.1
Posts: 64
Rep:
|
My take on this is to use the cut command, such as below;
Code:
#!/bin/sh
output=`echo $1 | cut -f 1 -d '.'`
cc -o $output $1
# exit
but then im slow and someone else got there before me 
Last edited by samwwwblack; 01-31-2006 at 07:32 PM.
|
|
|
|
01-31-2006, 07:45 PM
|
#4
|
|
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 13.37
Posts: 4,084
Rep: 
|
I'm going to ignore the C specific stuff because I don't know anything about it
But, if you have a shell variable with a filename as its contents, e.g.
Code:
export MYVAR=newprog.c
To get just the newprog part, use a pattern matching operator, e.g.
|
|
|
|
01-31-2006, 08:40 PM
|
#5
|
|
Member
Registered: Aug 2004
Location: Argentina
Distribution: slackware 12
Posts: 211
Original Poster
Rep:
|
hey, thanks 4 the answers! *really* fast!! 
Booster: about the 'make' cmd.. I use to use it for the classic configure/make/make-install procedure to compile progs.. but here, when I give to it just one .c file as parameter (textually as you post), nothing is done .. I recreate the output:
Code:
~$ make myProg.c
make: No se hace nada para `myProg.c'
...and now..  ..translated:
Code:
make: nothing is done to `myProg.c'
-of course, 'make' doesn't create any binary file. It seems it's waitin' 4 something else to do it job.. I also do the 'make --help', but didn't found the -cyb --CompileYouBast#%&! option
...
About the scripts: gonna try'em out right now, and see which one fit better to my needs..
thanks again to all of you, pals!
|
|
|
|
01-31-2006, 09:42 PM
|
#6
|
|
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
|
for any extension,
Code:
echo <programname> | sed 's/\..*//'
|
|
|
|
01-31-2006, 10:41 PM
|
#7
|
|
Member
Registered: Sep 2003
Location: Ontario, Canada
Distribution: Gentoo, Suse
Posts: 59
Rep:
|
Quote:
|
Originally Posted by minike
Booster: about the 'make' cmd.. I use to use it for the classic configure/make/make-install procedure to compile progs.. but here, when I give to it just one .c file as parameter (textually as you post), nothing is done .. I recreate the output:
|
try "make myProg"
ie without the ".c"
I just tried (to be sure), and:
Code:
paul@pc vid [0]$ vi test.c
paul@pc vid [0]$ make test
cc test.c -o test
paul@pc vid [0]$
HTH
|
|
|
|
01-31-2006, 10:52 PM
|
#8
|
|
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 13.37
Posts: 4,084
Rep: 
|
Or, short and generic: ${MYVAR%.*} 
|
|
|
|
02-01-2006, 09:45 AM
|
#9
|
|
Member
Registered: Aug 2004
Location: Argentina
Distribution: slackware 12
Posts: 211
Original Poster
Rep:
|
yeps, Booster.. that's the way .. without extension.. 
Steve: just tried out your recipe, and it works just fine, too
I'm amazing about the many ways to perform such a "trivial" task like this
tx again, and happy coding! 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:02 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|