LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices


Reply
  Search this Thread
Old 01-29-2014, 06:35 AM   #1
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Rep: Reputation: 16
storing the output in two different variables


My problem is to separate the length output and the path output in two different variables.

How can I do it? I mean to say from the vairable "i" how to separate the o/p of wc -L and
find command?

Code:
#! /bin/bash

for i in `find . -name "*TEST*"|xargs wc -L`
	do
		echo $i
	done;
 
Old 01-29-2014, 06:46 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
A different approach would be better (just my 2c). Have a look at this example:
Code:
#!/bin/bash

while read LENGTH FILE
do
  echo "Length : ${LENGTH}"
  echo "File   : ${FILE}"
done < <( find . -name "*TEST*" | xargs wc -L)
 
Old 01-29-2014, 09:12 PM   #3
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Unhappy

I am getting the error while running the code:

syntax error near unexpected token done

Last edited by jone kim; 01-29-2014 at 09:12 PM. Reason: spelling correction
 
Old 01-30-2014, 03:00 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Quote:
Originally Posted by jone kim View Post
I am getting the error while running the code:

syntax error near unexpected token done
I just tried the script I posted (copy/pasted it) and it works on my side:
Code:
$ cat foo.sh
#!/bin/bash

while read LENGTH FILE
do
  echo "Length : ${LENGTH}"
  echo "File   : ${FILE}"
done < <( find . -name "*TEST*" | xargs wc -L )

$ ./foo.sh
Length : 0
File   :
If I change *TEST* to something that is present I get this:
Code:
$ ./foo.sh
Length : 79
File   : ./Bin/fife.sh.prev
Length : 91
File   : ./Bin/gsi.sh.prev
Length : 91
File   : total
Are you sure you did not make a typo somewhere?

Last edited by druuna; 01-30-2014 at 03:29 AM. Reason: Added missing )
 
Old 01-30-2014, 03:25 AM   #5
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
yes still I am getting the error. You have left " ) " in your code.

while doing sh foo.sh
getting the error: syntax error near unexpected token `<'
 
Old 01-30-2014, 03:33 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Quote:
Originally Posted by jone kim View Post
You have left " ) " in your code.
Sorry about that, fixed my previous code

Quote:
while doing sh foo.sh
getting the error: syntax error near unexpected token `<'
Why use sh to execute the script? That will create an error in this case.

Give the bash script execute permissions (chmod 750 foo.sh) and execute it like this: ./foo.sh
 
Old 01-30-2014, 03:38 AM   #7
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Thank You druuna

Please can you explain me why dont need to execute the script in this case? Why need to do chmod 750 foo.sh. Please explain me.

Sorry for the inconvenience.
 
Old 01-30-2014, 04:05 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Quote:
Originally Posted by jone kim View Post
Please can you explain me why dont need to execute the script in this case? Why need to do chmod 750 foo.sh. Please explain me.
There are a couple ways you can execute a file/script.

A script can be run by itself when:
- The script is made executable (which is done by the chmod 750 script_name command.
- The first line is a so called she-bang. In the case of bash that would look like: #!/bin/bash. A perl script for example would look like this: #!/usr/bin/perl. This first line tells Linux what must be used to execute the lines that follow.

If the above criteria are met you can execute the script by using ./script_name or /full/path/to/script_name or, if the script can be found in the PATH setting, by using only its name: script_name

Another way of executing a file/script is by specifying the command to use from the command line.
- No execute permissions are needed, the file/script must have read access.
- You do need to specify the correct command. sh, bash and perl for example are different. In your specific case you need bash and not sh (which is a more restricted form).

This would have worked: bash foo.sh and this sh foo.sh would not work.

Here's a link that explains the above in a different way: How to run a shell script in Linux
 
Old 02-01-2014, 01:03 AM   #9
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Thank You druuna very much for the nice explanation. Now the code is working. I got a very nice understanding from you.

I want to put you a silly question hope you will answer it with out being annoyed.

can you explain me the code, how is this working. I am confused because after "done", you have put the the linux command. How is it working?

Can we use a if loop suppose if the length of the file is greater than 100, echo "standard file format" else if length greater than 200 echo "non standard" else echo "redundant". Can we do it?
 
Old 02-01-2014, 03:10 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Quote:
Originally Posted by jone kim View Post
Thank You druuna very much for the nice explanation. Now the code is working. I got a very nice understanding from you.
You're welcome

Quote:
Originally Posted by jone kim
can you explain me the code, how is this working. I am confused because after "done", you have put the the linux command. How is it working?
The output generated by the find command needs to be given to the loop and there are more ways to do this. The while loop can also read information from a command like this:
Code:
find . -name "*TEST*" | xargs wc -L | while read LENGTH FILE
do
  echo "Length : ${LENGTH}"
  echo "File   : ${FILE}"
done
But the above isn't the preferred way, the one I gave in post #2 is preferred.

With a minimal adjustment you can also use the method used in post #2 to read in a file:
Code:
while read LENGTH FILE
do
  echo "Length : ${LENGTH}"
  echo "File   : ${FILE}"
done < input.file
Quote:
Originally Posted by jone kim
Can we use a if loop suppose if the length of the file is greater than 100, echo "standard file format" else if length greater than 200 echo "non standard" else echo "redundant". Can we do it?
The command used (this find .... | xargs ...) produces 2 parts (size and file name) per entry. Using a while loop you can put those parts in separate variables (size is stored in LENGTH and the file name in FILE). Once that is done you can easily work with these variables inside the while loop.

You can use a for loop inside the while loop to check these variables (LENGTH in your example) and print a message according to the found results. Here's an example:
Code:
while read LENGTH FILE
do
  if [ "$LENGTH" -lt 100 ]
  then
    # length is less than 100
    echo "${FILE} is redundant."
  elif [ "$LENGTH" -gt 200 ]
  then
    # length is greater than 200
    echo "${FILE} is non standard."
  else
    # length is between 100 and 200
    echo "${FILE} standard file format."
  fi
done < <( find . -name "*TEST*" | xargs wc -L | grep -v total )
I added the grep -v total command to make sure that the total line is discarded.

Here are some bash related links that might come in handy:
- BASH Programming - Introduction HOW-TO
- Bash Guide for Beginners
- Bash Pitfalls
- BASH Frequently Asked Questions
 
Old 02-04-2014, 03:53 AM   #11
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Question cut command error

I have multiple files eg. XML.TEST.UAG.txt; XML.TEST.DAG.txt;XML.TEST.UCG.txt
Code:
#!/bin/bash

while read LENGTH FILE
do
  echo "Length : ${LENGTH}"
  echo "File   : ${FILE}"
  if [ ${LENGTH} == 176 ];
	then filename=`{FILE}| cut -d "." -f3`
		echo filename	  
  fi
done < <( find . -name "*TEST*" | xargs wc -L)
I've to take the string which is between 2nd and 3rd part of the file name.

But I am getting error as:
cut: you must specify a list of bytes, characters, or fields.
Why is it so? what is wrong with this? In the variable {$FILE} we have characters.

Please clarify.

Thanks!
 
Old 02-04-2014, 05:35 AM   #12
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Exclamation transfer this question to the right forum

I think I've posted this in wrong forum.
Please transfer this post to the right place.

Thanks
 
Old 02-05-2014, 01:47 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Your question was answered in you other thread: https://www.linuxquestions.org/quest...me-4175493395/
 
Old 02-05-2014, 03:47 AM   #14
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Question

hello drunna thank you for your comment once again.

But I am not getting the desired results. I got the errors as:

this code runs for infinite times
Code:
#!/bin/bash

while read LENGTH FILE
do
  echo "Length : ${LENGTH}"
  echo "File   : ${FILE}"
  if [ ${LENGTH} == 176 ];
	then filename=`{$FILE}| cut -d "." -f3`
		echo filename	  
  fi
done < <( find . -name "*TEST*" | xargs wc -L)
this code is not working
Code:
#!/bin/bash

while read LENGTH FILE
do
  echo "Length : ${LENGTH}"
  echo "File   : ${FILE}"
  if [ ${LENGTH} == 176 ];
	then filename=$ sed -r 's/^([^.]+\.){4}(D[^.]+\.).*$/\2/' {$FILE}
		echo filename	  
  fi
done < <( find . -name "*TEST*" | xargs wc -L)
this code is not working
Code:
#!/bin/bash

while read LENGTH FILE
do
  echo "Length : ${LENGTH}"
  echo "File   : ${FILE}"
  if [ ${LENGTH} == 176 ];
	then filename=$ cut -d "." -f 6 {$FILE}
		echo filename	  
  fi
done < <( find . -name "*TEST*" | xargs wc -L)
I can not figure out where is the mistake. Please can you check it.

Last edited by jone kim; 02-05-2014 at 03:49 AM.
 
Old 02-06-2014, 04:14 AM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
I do see a mistake in the syntax used. If you want to echo the content of a variable you need to add a $ in front of the variable name:
Code:
echo filename    # This will print filename
echo $filename   # This will print the content of the variable filename
You also do this, which isn't correct:
Code:
filename={$FILE}
As a result of the above code snippet filename will be filled with the content of FILE surrounded by { and }:
Code:
$ FILE=xyz
$ echo {$FILE}
{xyz}

# the correct way:
$ echo ${FILE}
xyz
About your wish to cut out part of the file names that are found. Have a look at this, which assumes the example given in post #11:
Code:
$ cat jone.in
XML.TEST.UAG.txt
XML.TEST.DAG.txt
XML.TEST.UCG.txt

$ awk -F. '{ print $3 }' jone.in 
UAG
DAG
UCG
Have a look here for some awk related sites:
- Awk - A Tutorial and Introduction
- The GNU Awk User's Guide

Your code would look something like this:
Code:
#!/bin/bash

while read LENGTH FILE
do
  echo "Length : ${LENGTH}"
  echo "File   : ${FILE}"
  # only if exactly 176 long
  if [ ${LENGTH} == 176 ]
  then
    # fill filename with third field
    filename=$( echo ${FILE} | awk -F. '{ print $4 }' )
    echo "filename : ${filename}"
  fi
done < <( find . -name "*TEST*" | xargs wc -L | grep -v total )
 
  


Reply

Tags
shell script


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
Storing over 10 variables shoemoodoshaloo Linux - Newbie 5 07-08-2009 09:32 AM
Storing USB device name in separate variables dynamically. kushalkoolwal Programming 2 01-29-2009 01:05 PM
PHP: storing all data form a db row in variables with the same name as the db column konqi Programming 2 07-10-2008 05:13 AM
another simple unix scripting question! storing values in variables christianunix Linux - Newbie 4 10-30-2007 01:13 PM
Tk: Storing Output of Program Kenji Miyamoto Programming 4 06-25-2005 01:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > General

All times are GMT -5. The time now is 09:22 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration