Hattori
Hattori

Reputation: 3

Bash - piping awk result to variable

I'm new to bash, and I've literally spent hours trying to figure this out but I'm stuck.

I'm writing a script which will auto-execute upon completion of a download in pyLoad. I need to check if the first word of the package name is "Public".

Whilst trying to debug, I've gotten this so far:

#!/bin/sh
PACKAGE="$1"
PATH="$2"

FIRST=$(echo $PACKAGE|awk '{print $1}')
echo "First word is: $FIRST"

Running this by means of sh download.sh "test package" ~/ returns

download.sh: 5: download.sh: awk: not found

I get the same result whether "test package" is in quotes or not.

My aim is to get to something like this:

if [ $FIRST == "public"  ]
then
    # Move to public folder
else
    # Do nothing
fi

Any help would be appreciated.

OS: Ubuntu 12.04 x64

PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Upvotes: 0

Views: 234

Answers (2)

Yuriy Nazarov
Yuriy Nazarov

Reputation: 1656

awk: not found

because you redefine PATH variable. Try use another name for your internal PATH variable.

Upvotes: 2

konsolebox
konsolebox

Reputation: 75458

You can have other options:

[[ $PACKAGE =~ ^[[:space:]]*([^[:space:]]+) ]]
FIRST=${BASH_REMATCH[1]}
echo "First word is: $FIRST"

Or

FIRST=${PACKAGE%%[[:space:]]*}
echo "First word is: $FIRST"

Upvotes: 0

Related Questions