LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 01-31-2006, 07:52 PM   #1
minike
Member
 
Registered: Aug 2004
Location: Argentina
Distribution: slackware 12
Posts: 211

Rep: Reputation: 30
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,
 
Old 01-31-2006, 08:16 PM   #2
Booster
Member
 
Registered: Sep 2003
Location: Ontario, Canada
Distribution: Gentoo, Suse
Posts: 59

Rep: Reputation: 15
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
 
Old 01-31-2006, 08:30 PM   #3
samwwwblack
Member
 
Registered: Apr 2003
Location: Midlands, UK
Distribution: Lapwing-Linux 2009.1
Posts: 64

Rep: Reputation: 15
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 08:32 PM.
 
Old 01-31-2006, 08:45 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
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.

Code:
echo ${MYVAR%.c}
 
Old 01-31-2006, 09:40 PM   #5
minike
Member
 
Registered: Aug 2004
Location: Argentina
Distribution: slackware 12
Posts: 211

Original Poster
Rep: Reputation: 30
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!
 
Old 01-31-2006, 10:42 PM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
for any extension,

Code:
echo <programname> | sed 's/\..*//'
 
Old 01-31-2006, 11:41 PM   #7
Booster
Member
 
Registered: Sep 2003
Location: Ontario, Canada
Distribution: Gentoo, Suse
Posts: 59

Rep: Reputation: 15
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
 
Old 01-31-2006, 11:52 PM   #8
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
Or, short and generic: ${MYVAR%.*}
 
Old 02-01-2006, 10:45 AM   #9
minike
Member
 
Registered: Aug 2004
Location: Argentina
Distribution: slackware 12
Posts: 211

Original Poster
Rep: Reputation: 30
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!
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 11:35 AM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 11:47 AM
When "function pointer" meets "template"... I can't get rid of this compiling er cyu021 Linux - Software 3 12-17-2004 08:52 PM
using java to find the location of the "\" char in a string caged Programming 8 02-03-2004 11:42 AM
"Function not imlemented" error in call to "sem_open()" Krishnendu8 Linux - Networking 0 06-07-2003 03:19 AM


All times are GMT -5. The time now is 05:25 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration