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, |
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//` HTH |
My take on this is to use the cut command, such as below;
Code:
#!/bin/sh |
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 Code:
echo ${MYVAR%.c} |
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 Code:
make: nothing is done to `myProg.c' ... 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! |
for any extension,
Code:
echo <programname> | sed 's/\..*//' |
Quote:
ie without the ".c" I just tried (to be sure), and: Code:
paul@pc vid [0]$ vi test.c |
Or, short and generic: ${MYVAR%.*} :)
|
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. |