LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-23-2012, 07:19 AM   #1
debianolly
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Rep: Reputation: Disabled
Im having a problem with my script that copys paths one file and cats it into another


#!/bin/bash

echo 'this script is for sourcepaths'
echo 'please put name of file?'
read ans

echo $ans

catintofile()
{
sed -n '/WANT/,/END/p' zenon_start_release_rhel5_only.sh3 | awk '{print $1,$2,$3}' | cat >> ${ans}
}



catintofile2()
{
sed -n '/WANT/,/END/p' zenon_start_release_rhel5_only.sh3 | awk '{print $3}' | cat >> ${ans}
}

~
Please help me this one doesnt seem to be working
 
Old 04-23-2012, 07:33 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,063

Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
- first, use code tags to make it more readable.

- b, you never call the defined functions catintofile and catintofile2.

- iii, what are you expecting the output of sed -n '/WANT/,/END/p' (i think it does the same thing as grep WANT) ?

also, print out a few lines from zenon_start_release_rhel5_only.sh3 so we know what the input looks like. we mite be able to help you a little better if we know what you are dealing with (give us a before and after picture).
 
Old 04-23-2012, 09:13 AM   #3
debianolly
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi yeah sorry I want to cat all the paths between WANT and END into another file. This is an example of the file.
WANT
/bin/cp /build/redhat/opt/zenon/public_html/WEB-INF/lib/*jar /opt/zenon/zenonserver/lib
/bin/cp /build/redhat/opt/zenon/zenonserver/lib/*jar /opt/zenon/zenonserver/lib
cd /root/NS
END
export PATH=/root/jet/jet7.0-pro/bin:$PATH
export LD_LIBRARY_PATH=/root/jet/jet7.0-pro/lib/x86/shared:$LD_LIBRARY_PATH
echo "changing permission of build_NS.sh script"
chmod +x build_NS.sh
echo "Running build_NS.sh script...."
/bin/rm -rf /root/NS/NS3-native/*
./build_NS.sh
echo "Done"
xpack NS3.jpn
cp /root/NS/NS3-native/NS3 /build/redhat/opt/zenon/zenonserver/.
cp -r /root/NS/NS3-native/rt /build/redhat/opt/zenon/zenonserver/.
WANT
bring "$1" "/opt/zenon/zenonserver/heller.keystore" "$2"
bring "$1" "/opt/zenon/zenonserver/jetty-zenon.xml" "$2"
bring "$1" "/opt/zenon/zenonserver/loadtest_qcif.flv" "$2"
bring "$1" "/opt/
The file is so big i wouldnt be able to show you the rest of it
 
Old 04-23-2012, 09:16 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Just saying "it's not working" doesn't really explain your problem now, does it? Why not explain what it is doing that it shouldn't, and what it isn't doing that it should?


To start with, some general tips:

Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).

Many people also think that it's more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as it more clearly separates the outside block from the inside block.

Code:
for var in fee fai foo fum ; do

	if [[ "$var" == "foo" ]]; then
		echo "Found 'foo'."
	fi

done
The brackets on a function definition would also apply here.


QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes



Now, as for this line...

Code:
sed -n '/WANT/,/END/p' zenon_start_release_rhel5_only.sh3 | awk '{print $1,$2,$3}' | cat >> ${ans}
To start from the end, the final cat command is completely superfluous here. You can redirect the output directly into the file without it.

Second, there's usually no need to use both sed (or grep) and awk together. awk can do everything sed can and more. Or it's likely in this case that even sed can do it alone, depending on the exact nature of the file input and the desired output.

Third, it's probably a good idea to store the name of the input file in a variable too. File names and other external data points should usually not be hard-coded into a script.

Fourth, be sure to quote all variable substitutions, as mentioned. However, using the full ${var} form when you don't need it really only adds clutter to the script. Leave them off.


I believe this should do it for your first function, for example:
Code:
infile='zenon_start_release_rhel5_only.sh3'

catintofile() {
	awk '/WANT/,/END/ { print $1,$2,$3 }' "$infile" >> "$ans"
}

Finally, I highly suggest setting up some kind of check on the input string to make sure you got a proper/desirable file name before actually trying to use it.
 
Old 04-23-2012, 09:43 AM   #5
debianolly
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
sorry what you just gave me is a big help but when I execute the script the paths i want between WANT and END are not in the desired file. There are two sections in the zenon_start_release_rhel5_only.sh3 file that i have marked with WANT at the start and END at the end. Is there a way where you can use a while loop to loop through both sections. The problem is that i want $1 $2 $3 in the first section and i only want $3 in the second section. I was thinking somthing like this:
awk '/WANT/,/END/p' {if($bring) print $3 else print $1,$2,$3} 'infile'>>$ans
i modified the script to this it is still not working.
#!/bin/bash

echo 'this script is for sourcepaths'
echo 'please put name of file?'
read ans

echo $ans

infile='zenon_start_release_rhel5_only.sh3'

##This function is for the first set of paths
srcepths(){
awk '/WANT/,/END/ { print $1,$2,$3 }' "$infile" >> "$ans"
}


##This function is for the second set of paths
srcepths2(){
awk '/WANT/,/END/ { print $3 }' "$infile" >> "$ans"
}
 
Old 04-23-2012, 02:26 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,063

Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
i dont understand your description and you still havent given us an example of the desired output.
 
  


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
Changing paths with eval and a script xafwodahs Linux - General 1 03-02-2012 09:23 PM
script to read paths Christos Badogias Linux - Newbie 11 11-06-2011 08:51 AM
firefox plugin which copys css location true_atlantis Linux - Software 1 07-20-2006 11:55 PM
Creating a bash script which understands paths to files en8scl Linux - Newbie 9 05-21-2006 07:22 PM
Automatically resolving WINDOWS paths to pre-configured Linux paths gazzy Linux - General 1 09-05-2003 10:15 PM


All times are GMT -5. The time now is 12:59 PM.

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