SunQuest
           Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old July 31st, 2007, 02:44 AM
htbeast htbeast is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 87 htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level)htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level)htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level)htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level)htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level)htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 8 m 28 sec
Reputation Power: 31
Bash script problem

Hi.
I have a problem with a bash script I need to write for a course. I'm not looking for someone to answer the question for me, I could just do with a little nudge in the right direction.

The task is pretty trivial, but the problem lies with the limited set of tools I have at my disposal. I have to write a bash script that takes two command line arguments, $1 being some text, and $2 being the name of a non-empty file. The script has to insert $1 into the middle of $2 (if it's even then round down).

The only file editing commands allowed are vi and cat; so no sed,ed,awk or anything that would be used in the real world. Also no control structures (if,for....). the only way I can think of doing it is by using vi from the script, but I can't find any clear guide on how to edit with vi from a script. Here is what I've got so far and a sort of rough algo of what I think needs to be done. Please excuse any syntax errors, I'm very new to this but I hope it's clear enough to see what I'm I'm attempting.

Code:
#shbang line

lines=wc -l |cut -d' ' -f1;
midline=$lines / 2;

#open vi on 'midline' and insert text
vi +$midline $2
<A COMMAND TO INSERT $1>
<A COMMAND TO SAVE>
<A COMMAND TO EXIT>
echo "$1 inserted at $midline in $2"


I'd really appreciate it if someone who's not a noob like myself could tell me if I'm on the right track and perhaps point me to a reference or tutorial about doing simple editing with vi from a bash script.

Thanks very much.

Reply With Quote
  #2  
Old July 31st, 2007, 10:58 AM
xnemesis64 xnemesis64 is offline
Google's No1 Supporter!
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2007
Location: irc.superirc.info
Posts: 569 xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 4 Days 15 h 48 m 12 sec
Reputation Power: 145
Send a message via MSN to xnemesis64 Send a message via Yahoo to xnemesis64 Send a message via Google Talk to xnemesis64 Send a message via Skype to xnemesis64
You cannot use conditional statements? That makes it pretty damn difficult to determine it's a non-empty file etc. I presume from the specifics of this it's a homework assignment of some sort. Due to this I won't give you the solution, rather point you in some helpful directions.
*nix comes with a nice little operator (>) that will allow you to redirect output. This operator is destructive meaning it will overwrite any existing data that may exist where you are re-directing your output to. *nix also has a non-destructive redirection operator that will append instead of replace (>>). This is the important part to placing your input in to a text file.
In regards to Vi, the important thing to remember is it's a text editor. And that is what it does, allows the user to edit text. It does not provide commands or parameters to pass data that can be input in to a file. Unfortunately this is not its intended purpose, so it is useless for this. Looking at your problem Vi is about as useful as a chocolate teapot to it. I suggest you concentrate your efforts on using the above operators.

HTH
__________________
Did this post help? Please Click The Next To My Post
Need help? Did you try Google?
Take a look over at my current work in progress http://crispycrisp.org

Reply With Quote
  #3  
Old August 2nd, 2007, 11:31 AM
htbeast htbeast is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 87 htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level)htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level)htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level)htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level)htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level)htbeast User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 8 m 28 sec
Reputation Power: 31
Thanks for your reply. In the end I just used 'head -n' and 'tail -n' together with 'cat'. I had to use an 'if' statement to account for files with and odd number of lines, but I couldn't see another way around it.

Reply With Quote
  #4  
Old August 3rd, 2007, 04:23 AM
xnemesis64 xnemesis64 is offline
Google's No1 Supporter!
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2007
Location: irc.superirc.info
Posts: 569 xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 4 Days 15 h 48 m 12 sec
Reputation Power: 145
Send a message via MSN to xnemesis64 Send a message via Yahoo to xnemesis64 Send a message via Google Talk to xnemesis64 Send a message via Skype to xnemesis64
Quote:
Originally Posted by htbeast
Thanks for your reply. In the end I just used 'head -n' and 'tail -n' together with 'cat'. I had to use an 'if' statement to account for files with and odd number of lines, but I couldn't see another way around it.

You cannot achieve this without atleast one IF statement. I'd suggest more than one to check the file is not empty and/or that it exists.

Reply With Quote
  #5  
Old August 17th, 2007, 08:41 PM
klobster klobster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 2 klobster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 30 sec
Reputation Power: 0
dupe.

Reply With Quote
  #6  
Old August 17th, 2007, 08:43 PM
klobster klobster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 2 klobster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 30 sec
Reputation Power: 0
I wrote a quick script to prove that you can find the middle of file without using an if:
# $1 is the number of lines in your file:
number=$(( (($1 * 10) / 2) ))
answer=${number:0:$(( ${#number} - 1 ))}

echo "number is: $answer"
EOF

simple and effective.
BTW: to help you with the rest of your issue: cat -n will print line numbers.

Reply With Quote
  #7  
Old August 18th, 2007, 04:31 AM
xnemesis64 xnemesis64 is offline
Google's No1 Supporter!
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2007
Location: irc.superirc.info
Posts: 569 xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level)xnemesis64 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 4 Days 15 h 48 m 12 sec
Reputation Power: 145
Send a message via MSN to xnemesis64 Send a message via Yahoo to xnemesis64 Send a message via Google Talk to xnemesis64 Send a message via Skype to xnemesis64
Quote:
Originally Posted by klobster
I wrote a quick script to prove that you can find the middle of file without using an if

Not the entire scenario:
Quote:
Originally Posted by klobster
$2 being the name of a non-empty file

You need some form of conditional statement to check if it's a non-empty (already exists) file.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Bash script problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway