LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 04-22-2003, 11:19 PM   #1
dimavo
LQ Newbie
 
Registered: Apr 2001
Location: Sydney, Australia
Distribution: CentOS
Posts: 17

Rep: Reputation: 0
Red face Shell scripting & piping


Hey all,

I really want to sleep right now, so if you see me wondering off the track - I warned you. )

(5 minutes later...)
I have started a script for compiling my C programs. I want to find out how I can name the output file from gcc the same thing only without the .c suffix (e.g. I have source file "code.c" and I want executable called "code").

Below is the code I got. I know it is wrong (as it does not work ).

Also, "crp" is my own program that produces a trimmed string output (in the code below, I leave all the prefix, but delete 2 characters from the suffix (.c))



#!/bin/sh

case $# in
"1") crp $1 0 2 | gcc -Wall -ansi -pedantic $1 -o ;
"2") gcc -Wall -ansi -pedantic $1 -o $2 ;;
*) echo "USAGE: $0 filename | filename <filename>"
esac
 
Old 04-22-2003, 11:44 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Gidday mate :)

If you want to do it in bash (rather than perl, for example)
you should get the Advanced-Bash-Scripting Guide
from the linux documentation project homepage.

Cheers,
Tink
 
Old 04-23-2003, 04:30 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,140
Blog Entries: 54

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
Hasn't really got to do with advanced bash scripting IMHO, just strip the extension using "basename": name0=code.c name1=$(basename ${name0} .c).
 
Old 04-23-2003, 02:53 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
I thought the advanced scripting guide would be the right
thing since he completely misunderstood the purpose of
case ... as for the stripping: he's using his own tool for that.

Cheers,
Tink
 
Old 04-23-2003, 11:10 PM   #5
dimavo
LQ Newbie
 
Registered: Apr 2001
Location: Sydney, Australia
Distribution: CentOS
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks both of you guys!
I will have to learn advanced bashing for my course anyways, for now I went with learning some of the basic bashing with 'basename'

it now looks like this:

#!/bin/sh

case $# in
"1") name1=$(cropstr $1 0 2);
gcc -Wall -ansi -pedantic $1 -o $name1 ;;

"2") gcc -Wall -ansi -pedantic $1 -o $2 ;;
*) echo "USAGE: $0 filename | filename <filename>"
esac



Cheers!

Last edited by dimavo; 04-23-2003 at 11:19 PM.
 
Old 04-24-2003, 05:16 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,140
Blog Entries: 54

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
as for the stripping: he's using his own tool for that.
Yeah, ure right. I didn't read the post right.
 
Old 04-24-2003, 06:25 AM   #7
doublefailure
Member
 
Registered: Mar 2002
Location: ma
Distribution: slackware
Posts: 747

Rep: Reputation: 30
shouldn't this be covered by 'makefile'?
just a guess
 
Old 04-24-2003, 01:35 PM   #8
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Using make would be a much better way to build your sources. It's better to start learning it now before your projects get bigger.
 
Old 04-24-2003, 05:27 PM   #9
mhearn
Guru
 
Registered: Nov 2002
Location: Durham, England
Distribution: Fedora Core 4
Posts: 1,565

Rep: Reputation: 47
Try:

#!/bin/bash
gcc -o `echo $1 | sed 's/\.c$//'` $1

then: fooscript whatever.c
 
  


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
Shell scripting: Print output to logfile, error to logfile & screen stefanlasiewski Programming 18 05-22-2008 12:47 PM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
piping to and 'error' window from termalan (shell script) dna3e8 Linux - Software 2 03-19-2005 11:08 AM
bash shell scripting - && and || gui10 Programming 10 12-15-2001 03:37 PM


All times are GMT -5. The time now is 09:38 PM.

Main Menu
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