Does anyone know bash scripting?

Linux specific questions, problems.
sledgehammer_999
Administrator
Administrator
Posts: 2443
Joined: Sun Jan 23, 2011 1:17 pm

Re: Does anyone know bash scripting?

Post by sledgehammer_999 »

Thanks for the alternative way. I'll take into consideration when I'll commence hacking on the unix build system.
pmzqla

Re: Does anyone know bash scripting?

Post by pmzqla »

[quote="sledgehammer_999"]
Thanks for the alternative way. I'll take into consideration when I'll commence hacking on the unix build system.
[/quote]

I wrote it mostly because the other solution posted is not working for me and since I'm not good with awk, I rewrote an alternative solution using sed and cut.
PS: I re-modified the script so that it works with paths containing " -", although I had to make a more complex regular expression.

[quote="sledgehammer_999"]
I wonder if it is possible to auto-create the 3 variables like m4 scripts do in configure.ac.
eg Let's say that you pass only CFLAGS to extract(). extract() parses $1 and creates these 3 vars: $1_INCLUDEPATH, $1_DEFINES and $1_OTHER. So if you pass CFLAGS you get CFLAGS_INCLUDEPATH, CFLAGS_DEFINES and CFLAGS_OTHER.
Is it that possible? I don't if bash has similar feature to the m4 language.
[/quote]

And to answer this question. You need eval to do it, but I don't think it's something commonly done.
Example:

Code: Select all

PREFIX=whatever
eval ${PREFIX}_DEFINES=content
echo $whatever_DEFINES  #outputs 'content'
sledgehammer_999
Administrator
Administrator
Posts: 2443
Joined: Sun Jan 23, 2011 1:17 pm

Re: Does anyone know bash scripting?

Post by sledgehammer_999 »

@littletree76

Unfortunately the only script from the ones you have provided is this one:

Code: Select all

#!/bin/sh

awk=/usr/bin/awk
tee=/usr/bin/tee

### Assigment of variable to be processed.

CFLAGS='xxx -I/usr/local/include/libtorrent -DRANDOM_DEF yyy -I/usr/local/include/boost -DRANDOM_DEF2=456 zzz'


### Processing start here.
### Cut and paste between the two dotted comment lines.

### -------------------------------------------------------------------------------------------------------------

VAR1=`echo $CFLAGS | $awk -F" " '{ for ( i=1; i<=NF; i++ ) if ( $i ~ /^-I/ ) printf "%s ", $i }'`
VAR2=`echo $CFLAGS | $awk -F" " '{ for ( i=1; i<=NF; i++ ) if ( $i ~ /^-D/ ) printf "%s ", $i }'`
VAR3=`echo $CFLAGS | $awk -F" " '{ for ( i=1; i<=NF; i++ ) if ( $i !~ /^-I/ && $i !~ /^-D/ ) printf "%s ", $i }'`

### Strip away extra characters.

VAR1=`echo $VAR1 | $awk '{ gsub("-.| $", "", $0); print }'`
VAR2=`echo $VAR2 | $awk '{ gsub("-.| $", "", $0); print }'`
VAR3=`echo $VAR3 | $awk '{ gsub(" $", "", $0); print }'`



echo $VAR1
echo "--------------------"
echo $VAR2
echo "--------------------"
echo $VAR3
echo "--------------------"
The others make the 3 variables have the SAME value which is "xxx yyy zzz"

@pmzqla

This code

Code: Select all

#!/bin/sh

# $1: String to parse
# Set $DEFINES, $INCLUDES, $OTHER
extract() {
  if [ -z "$1" ]; then
    echo "Input string required"
    return 1
  fi

  # Convert " -" to "\n" if not between quotes
  string=$(echo " "$CFLAGS | sed -e 's: -:\n:g' -e 's:"\(.*\)\n\(.*\)":\"\1 -\2":g' -e "s:'\(.*\)\n\(.*\)':\'\1 -\2':g")
  SAVEIFS=$IFS
  IFS=$(printf "\n\b")
  for i in $string; do
    case "$(echo "$i" | cut -c1)" in
      '') ;;
      D) VAR2="$(echo $i | cut -c2-) $VAR2";;
      I) VAR1="$(echo $i | cut -c2-) $VAR1";;
      *) VAR3="-$i $VAR3";;
    esac
  done
  IFS=$SAVEIFS
}

CFLAGS='xxx -I/usr/local/include/libtorrent -DRANDOM_DEF yyy -I/usr/local/include/boost -DRANDOM_DEF2=456 zzz'
extract $CFLAGS



echo $VAR1
echo "--------------------"
echo $VAR2
echo "--------------------"
echo $VAR3
echo "--------------------"
Outputs

Code: Select all

/usr/local/include/boost /usr/local/include/libtorrent
--------------------
RANDOM_DEF2=456 zzz RANDOM_DEF yyy
--------------------
- xxx
--------------------
As you can see zzz and yyy don't belong in the defines.
pmzqla

Re: Does anyone know bash scripting?

Post by pmzqla »

[quote="sledgehammer_999"]
-- cut --

Outputs

Code: Select all

/usr/local/include/boost /usr/local/include/libtorrent
--------------------
RANDOM_DEF2=456 zzz RANDOM_DEF yyy
--------------------
- xxx
--------------------
As you can see zzz and yyy don't belong in the defines.
[/quote]
I removed a comment in the last edit by mistake. The code relies on the fact that each flag starts with "-", which AFAIK is true.

EDIT:
Try with:

Code: Select all

CFLAGS='-xxx -I/usr/local/include/libtorrent -DRANDOM_DEF -yyy -I/usr/local/include/boost -DRANDOM_DEF2=456 -zzz'
Last edited by pmzqla on Tue Nov 18, 2014 11:39 pm, edited 1 time in total.
sledgehammer_999
Administrator
Administrator
Posts: 2443
Joined: Sun Jan 23, 2011 1:17 pm

Re: Does anyone know bash scripting?

Post by sledgehammer_999 »

[quote="pmzqla"]
[quote="sledgehammer_999"]
-- cut --

Outputs

Code: Select all

/usr/local/include/boost /usr/local/include/libtorrent
--------------------
RANDOM_DEF2=456 zzz RANDOM_DEF yyy
--------------------
- xxx
--------------------
As you can see zzz and yyy don't belong in the defines.
[/quote]
I removed a comment in the last edit by mistake. The code relies on the fact that each flag starts with "-", which AFAIK is true.

EDIT:
Try with:

Code: Select all

CFLAGS='-xxx -I/usr/local/include/libtorrent -DRANDOM_DEF -yyy -I/usr/local/include/boost -DRANDOM_DEF2=456 -zzz'
[/quote]

It works now.
I suppose that all stuff in CFLAGS will probably start with "-". I can't imagine a valid compiler switch not starting with "-"
Post Reply