LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash cripting function to "identify" any char in a string? (https://www.linuxquestions.org/questions/programming-9/bash-cripting-function-to-identify-any-char-in-a-string-410174/)

minike 01-31-2006 06:52 PM

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,

Booster 01-31-2006 07:16 PM

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

samwwwblack 01-31-2006 07:30 PM

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 :rolleyes:

gilead 01-31-2006 07:45 PM

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}

minike 01-31-2006 08:40 PM

hey, thanks 4 the answers! *really* fast!! :D
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.. :rolleyes: ..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!

kshkid 01-31-2006 09:42 PM

for any extension,

Code:

echo <programname> | sed 's/\..*//'

Booster 01-31-2006 10:41 PM

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

gilead 01-31-2006 10:52 PM

Or, short and generic: ${MYVAR%.*} :)

minike 02-01-2006 09:45 AM

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! ;)


All times are GMT -5. The time now is 08:05 AM.