LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-21-2009, 02:50 PM   #16
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713

The line "$files = $filedeps" doesn't work!?!?
 
Old 12-21-2009, 04:12 PM   #17
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Code:
#!/bin/bash
files=$*

getdeps(){
   filedeps=$(grep '#include' $file|sed 's:#include *[<"]::'|sed 's:[>"]::'|uniq)
   echo $filedeps|sed 's/ /\n/g'|while read file
   do
      echo $file
      if [ -f $file ]
      then
         getdeps
      fi
   done
}

echo $files|sed 's/ /\n/g'|while read file
do
   if [ -f $file ]
   then
      echo $file
      getdeps
   fi
done
 
Old 12-21-2009, 04:51 PM   #18
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by Disillusionist View Post
Code:
#!/bin/bash
files=$*

getdeps(){
   filedeps=$(grep '#include' $file|sed 's:#include *[<"]::'|sed 's:[>"]::'|uniq)
   echo $filedeps|sed 's/ /\n/g'|while read file
   do
      echo $file
      if [ -f $file ]
      then
         getdeps
      fi
   done
}

echo $files|sed 's/ /\n/g'|while read file
do
   if [ -f $file ]
   then
      echo $file
      getdeps
   fi
done
I really wish that there would be some better bash tutorial that actually explains how it works, because it is so not like normal languages, as it seems to be made of programs stuck together instead of predefined constructs.

for example I am used to "normal" programming languages and this completely freaks me out:

Code:
echo $files|sed 's/ /\n/g'|while read file
do
And, of course, the way you cannot just set the value of a variable.

The problem with most bash tutorials is that they treat it as a normal programming language, not even saying that things like the fact that "while" is actually independent command-line program that takes input and output, not a built-in construct.

Last edited by MTK358; 12-21-2009 at 04:56 PM.
 
Old 12-21-2009, 05:10 PM   #19
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
uh, while is a built-in construct. The problem is that piping creates subshells, and stuff that happens inside a subshell doesn't effect the parent.

You're right that pretty much all bash resources are misleading, especially ABS..
http://mywiki.wooledge.org/BashGuide
..much better

Disillusion's script is more POSIX shell than Bash, but even there it's got problems, particularly with handling filenames with spaces.

Code:
#!/usr/bin/env bash

getdeps() {
   while read -r file ; do
      echo "$file"
      getdeps "$file"
   done < <(grep -h '^#include *".*"$' "$1" | sed 's/^#include *"\(.*\)"$/\1/')
}

getdeps "$1"

Last edited by tuxdev; 12-21-2009 at 05:11 PM.
 
Old 12-21-2009, 06:39 PM   #20
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by MTK358 View Post
It would also be nice if it could recursively find the dependencies of these headers, and so on.
what do you mean by "dependencies"? can you show examples?
 
Old 12-21-2009, 07:12 PM   #21
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by tuxdev View Post
You're right that pretty much all bash resources are misleading, especially ABS..
http://mywiki.wooledge.org/BashGuide
..much better
[/code]
Wow, that tutorial is so, so much better than the "Advanced Bash Scripting Guide" advertised everywhere. It actually makes some sense!

Quote:
Originally Posted by ghostdog74 View Post
what do you mean by "dependencies"? can you show examples?
I mean .h files that the .c file includes.
 
Old 12-21-2009, 07:33 PM   #22
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk 'BEGIN{s="find /usr/include -type f \\( "}
/include/{
    gsub(/\042|[<>]/,"",$2)
    s=s"-iname "$2" -o "
}
END{
    gsub(/-o $/,"\\)",s)
    while( ( s | getline found) > 0 ) {
        print "found: "found
    }
}' file.c
 
Old 12-21-2009, 07:36 PM   #23
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Here's my current version, but the highlighted part doesn't seem to take effect:

Code:
#!/usr/bin/env bash

deps=''

getdeps() {
	while read -r file ; do
		deps=$(echo -e "$deps""$file"'\n')
		getdeps "$file"
	done < <(grep '#include *".*"' "$1" | sed 's:[^"]*"\([^"]*\)"[^"]*:\1:')
}

getdeps "$1"
echo -n "$(echo "$deps" | uniq | sort)"

Last edited by MTK358; 12-21-2009 at 07:38 PM.
 
Old 12-21-2009, 07:38 PM   #24
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
Actually this is supposed to be a part of an automated script that checks which .c files must be compiled.

For this purpose, I want it so that you just apply a .c file as the argument and it lists out all the .h files it includes.
This is half the reason I use automake and autoconf. If you insist on rewriting them yourself, you might as well look at the source code.
Kevin Barry
 
Old 12-21-2009, 08:33 PM   #25
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
Use arrays.
Code:
#!/usr/bin/env bash

deps=()

getdeps() {
	while read -r file ; do
                deps+=("$file")
		getdeps "$file"
	done < <(grep '#include *".*"' "$1" | sed 's:[^"]*"\([^"]*\)"[^"]*:\1:')
}

getdeps "$1"
printf "%s\n" "${deps[@]}" | sort | uniq
I do share ta0kira's concerns over why you are trying to resolve this problem (I prefer use CMake over autotools, though). I also sometimes use ntubski's suggestion to use gcc's -MD for simpler projects. After all, make's job is to resolve dependencies, but you have to actually tell make what they are.
 
Old 12-22-2009, 08:23 AM   #26
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
It works now.
 
  


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
list all dependencies when one, or more, blocked by 'masked by: ~amd64 keyword'? zymos Gentoo 2 08-18-2009 10:28 PM
yum and packagekit - list installed packages and dependencies allele Linux - Newbie 2 06-24-2009 03:31 PM
Newbie: Can't figure out dependencies list for superkaramba on Slack 12. waco-old-paco Slackware 7 11-30-2007 11:52 AM
libc6 2.4-1 needed to complete list of dependencies for libfame-0.9_0.9.1-0.2_i386.de wabbalee Linux - Desktop 4 10-11-2006 09:07 PM
Sources list and Packages dependencies varaonsite Debian 2 07-31-2006 04:43 PM


All times are GMT -5. The time now is 06:31 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