Trying to embed a pipe with a variable IN a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to embed a pipe with a variable IN a variable
# 1  
Old 05-27-2010
Question Trying to embed a pipe with a variable IN a variable

Hey all,

I've tried this for quite some time now and haven't figured it out... I was hoping one of you shell scripters could help a newbie out Smilie

I am trying to take the 1st parameter of my script and use it as the search pattern for my for loop

Code:
#!/usr/bin/sh
set -vx
REGEX=$1
 
GREP="| grep $REGEX" <-- I can't seem to get the syntax correct here
 
 
for i in `ls -l "$GREP"| awk '{ print $5 }'`
do
  if [ ${total} -eq 0 ]; then
    total=$i
  else
    total=`expr $i + $total`
  fi
done
final=`expr $total / 1024`
echo $final in KB

Thanks a bunch guys; anyone know of any good fundamental scripting sites for shell?

Last edited by Scott; 05-27-2010 at 08:23 PM.. Reason: Code tags, please...
# 2  
Old 05-27-2010
Code:
ls -l | nawk '$0 ~ regex {tot+=$5}END{print tot/1024 " in KB"}' regex='.sh'

# 3  
Old 05-27-2010
Code:
for i in `ls -l "$GREP"| awk '{ print $5 }'`

So long as the pipe is within the $GREP variable, it's impossible to do what you're attempting without sending the results of the variable expansion through the sh parser using eval. To implement that pipeline that you're attempting, I would suggest:
Code:
for i in `ls -l | grep "$REGEX" | awk '{ print $5 }'`

Regards,
Alister
# 4  
Old 06-01-2010
Thanks for the help! Smilie

Now I can use grep pattern search (I am trying to get egrep pattern to work) to determine size of files in a directory in kb/mb/gb!

Code:
#!/usr/bin/sh
#set -vx
 
for _params in $*
do
case ${_params} in
-search)
pattern="$2"
shift 2
;;
-size)
size="$2"
shift 2
;;
esac
done
 
GREP_CMD="/usr/bin/grep"
 
if [ -z "${pattern}" ]; then
echo "You'll need to specify a valid grep pattern (no regex!)"
exit 1
fi
 
for i in `ls -l | $GREP_CMD "$pattern" | awk '{ print $5 }'`
do
 
if [ "${total}" -eq 0 ]; then
total=$i
else
total=`expr $i + $total`
fi
done
final=`expr "$total" / 1024`
case $size in
kb|KB)
final=`expr "$total" / 1024`
;;
mb|MB)
final=`expr "$total" / 1024 / 1024`
;;
gb|GB)
final=`expr "$total" / 1024 / 1024 / 1024`
;;
esac
 
echo $final$size

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help on passing an input variable to the voronota program (after third pipe)

Dear UNIX forum members, I am using macbook pro 13 (2015 edition) with MAC OS Mojave and am trying to write the shell script where when it is run through terminal it asks for an input (in the code below an input variable is domains) and then that input becomes capital letter or letters which... (3 Replies)
Discussion started by: Aurimas
3 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

Pipe to awk to variable

Hi! If I'm trying something like: echo "hello world" | myvar=`awk -F "world" '{print $1}'` echo $myvar myvar is always empty :confused: I googled for houres now and don't understand why it isn't working... Trying it in normal bash. Can someone explain it to me so I can say "Of course!... (8 Replies)
Discussion started by: al0x
8 Replies

5. Shell Programming and Scripting

a pipe within a variable

Hi, I am trying to run this command: ED_CMD="$Access_OPS $ED_Logs_Dir/access | $lgrep -v $Access_Err" $lgrep $ED_CMD However, i am getting an error "Failed to open |: Broken pipe at " My question is how to put a pipe within a variable. Thanks. John. (4 Replies)
Discussion started by: john_prince
4 Replies

6. Shell Programming and Scripting

Assign command (with pipe) output to a variable

Hi , I would like to assign command (with pipe) output to a variable. The code is as follows. The goal of the code is to get the last folder folder with a particular name pattern. myDate=`ls | grep 2009 | tail -1` echo "myDate=" $myDate However, in the presence of the pipe, the code... (3 Replies)
Discussion started by: jeff_cen
3 Replies

7. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

8. Shell Programming and Scripting

How can I use a pipe operator in a variable: OPTION="| command"?

I have a string of commands I am piping some data through and I want to allow command line switches to select which commands are used. I want to do something like this: OPTION="| command3" command1 -a -b c.txt | command2 -d -e $OPTION >result.txt I want to do it that way because OPTION may be... (1 Reply)
Discussion started by: KenJackson
1 Replies

9. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

10. Shell Programming and Scripting

bash: "undefined variable" and pipe

Hi, haven't found anything about this through searching, so may be a new topic: when doing this: set -o nounset set -o errexit find . -name "*.lib" | while read library; do echo ${libary} done echo "after while" I expect the script to exit within the while loop (because of nounset and... (6 Replies)
Discussion started by: nagaidhlig
6 Replies
Login or Register to Ask a Question