|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 PostNeed help? Did you try Google? Take a look over at my current work in progress http://crispycrisp.org |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
Quote:
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. |
|
#5
|
|||
|
|||
|
dupe.
|
|
#6
|
|||
|
|||
|
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. |
|
#7
|
||||
|
||||
|
Quote:
Not the entire scenario: Quote:
You need some form of conditional statement to check if it's a non-empty (already exists) file. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Bash script problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|