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 05-24-2013, 01:15 AM   #1
shadownet
LQ Newbie
 
Registered: May 2013
Posts: 1

Rep: Reputation: Disabled
Question Shell Script to take 2 inputs and process the result


Hi, I am a bit newbie to shell scripting. I want to write a script which takes two inputs 1. Name of the file, 2. No of lines to grep. After taking this two inputs the script should grep no of lines (input 2) from the file (input 1) and put the results into a new file. The new filename can be <filename>.<current date>. So for example if i run the script:

# ./grepscript 100linesoflog.txt 10

This should create a new file called 100lineoflog.txt.20130524 which contains last 10 lines from 100linesoflog.txt

Also if 100lineoflog.txt.20130524 already existed then the new file should be created as 100lineoflog.txt.20130524-1 and if 100lineoflog.txt.20130524-1 exists then script should create 100lineoflog.txt.20130524-2 and so on.

Can someone plz help me write this script. Also since i want to learn please provide explanation on the contents of the script if possible.

Many thanks in advance
 
Old 05-24-2013, 01:22 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Start with these and see how you go
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

You would start the script with
Code:
#!/bin/bash

# optionally use next line to debug
set -xv
Note that a comment line starts with '#', UNLESS its the first (physical) line and starts '#!', which the kernel takes a as request to sue the named prog (/bin/bash) to execute the following content.

If you get stuck, post your code using code tags https://www.linuxquestions.org/quest...do=bbcode#code
 
Old 05-24-2013, 12:53 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
The description sounds suspiciously like homework to me.

As per the LQ rules we won't give you any direct solutions, but we can help guide you around problems. Please post the work you've done already and explain what you're hung up on.
 
Old 05-24-2013, 12:58 PM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,473

Rep: Reputation: 2539Reputation: 2539Reputation: 2539Reputation: 2539Reputation: 2539Reputation: 2539Reputation: 2539Reputation: 2539Reputation: 2539Reputation: 2539Reputation: 2539
Quote:
Originally Posted by shadownet View Post
Hi, I am a bit newbie to shell scripting. I want to write a script which takes two inputs 1. Name of the file, 2. No of lines to grep. After taking this two inputs the script should grep no of lines (input 2) from the file (input 1) and put the results into a new file. The new filename can be <filename>.<current date>. So for example if i run the script:

# ./grepscript 100linesoflog.txt 10

This should create a new file called 100lineoflog.txt.20130524 which contains last 10 lines from 100linesoflog.txt

Also if 100lineoflog.txt.20130524 already existed then the new file should be created as 100lineoflog.txt.20130524-1 and if 100lineoflog.txt.20130524-1 exists then script should create 100lineoflog.txt.20130524-2 and so on.

Can someone plz help me write this script. Also since i want to learn please provide explanation on the contents of the script if possible.
Please spell out your words. And as was said, we will be glad to help you, but we will not write it FOR YOU. Also, saying that you want to learn...so we should explain every step to you...is pretty transparent. That probably means your teacher wants you to explain/show your work.

If you really want to learn, then post what you've written/tried, and where you're stuck, and you'll gladly get help.
 
  


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
Create BASH script to process multiple inputs on read davisbase Linux - Newbie 5 05-26-2010 11:57 PM
shell script for a partial control over inputs to an external program njain Linux - Software 1 05-19-2010 07:57 PM
Hung script as a result of bg process Poki Linux - Newbie 1 05-01-2009 05:01 PM
shell script inputs aloishis89 Programming 3 03-10-2008 02:23 AM
Shell script to cd into the result of Find odinsride Programming 5 10-17-2007 10:33 AM


All times are GMT -5. The time now is 09:32 PM.

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