Register
It is currently Thu Dec 18, 2014 3:52 am

Could someone point me in the right direction?


All times are UTC - 6 hours


Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 PostPosted: Thu Nov 23, 2006 5:11 pm   

Joined: Mon Sep 18, 2006 1:16 pm
Posts: 27
Greetings!

I want to do some updates to some scripts I wrote and I want to make them more interactive. Could someone offer some ideas, advice, help and so forth?

My request is some examples with explainations on some ways to make scripts more interactive. Also a working checkforroot snippet would be nice. it seems the one most have refered to me dont work for me, i can still run the script as non-root on xubuntu. could be a typo or something on my part, but help with it would also be nice and appreciated.

thank you all

PS: also can someone give me working examples with explainations for how to use sed i think it is to find one line in a file and add 3 lines in place of it. despite all the research ive done on sed, i just cant seem to grasp it.


Top
 Profile  
 PostPosted: Fri Nov 24, 2006 3:35 pm   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
hmm to make them more interactive you can do things like:
Code:
read -p "your message here" variable

which takes user input and converts it into a variable.

A check for root I like to use is:
Code:
if [[ ! `whoami`=="root" ]]
then
          echo "you are not root"
fi

That basically checks the output of running the command whoami, and if it is not root (the ! is effectively a "not" sign), then it prints the echo command.

Can't help with sed - haven't grasped it meself yet ;)

HTH,
Rob


Top
 Profile WWW  
 PostPosted: Sun Dec 10, 2006 6:45 pm   

Joined: Wed Sep 06, 2006 12:19 pm
Posts: 54
Location: Covington, WA
katasuka wrote:
...PS: also can someone give me working examples with explainations for how to use sed i think it is to find one line in a file and add 3 lines in place of it. despite all the research ive done on sed, i just cant seem to grasp it.

The best tutorial (especially for beginners) I've come across is Bruce Barnett's UNIX Grymoire tutorial, Sed - An Introduction and Tutorial, which puts things into a nice, easy-to-learn perspective.....I highly recommend reading it....... :-)

For starters, you will need to read up on line addressing (locating a particluar line to edit) combined with one of three sed commands (not to be confused with the 'sed' options), which are "insert" (i), "append" (a) and "change" (c)........A brief summary of the commands are:
  • i: Insert a line - This command will insert text before the line specified in the line address. Also, this command will only accept a single address (line) to insert the text, as opposed to inserting text before an address range (sequence of multiple lines). This does not mean it will only work on one line and then quit, unless you're using line numbers for the line address. If using /regexp/ matching, it will insert the text before each line that matches the expression.
  • a: Append a line - This command will insert text after the matching line, and follows the same rules as the Insert command of working with a single line only, which actually makes sense since specifying a range of lines is rather pointless for just inserting/appending text.
  • c: Change a line - This command will replace a line, or multiple adjacent lines (range) with text. The best way to think of this command is to substitute the line(s) with text, similar to the commonly used "substitute" command (s///) but works with whole line(s) rather than snippets of text in a single line.
IMPORTANT: When adding multiple lines of text, each separate line must have a single backslash at the end of the line and must be the very last character of the line. This also includes the command itself if placing the replacement text on a separate line of it's own (ie., i\, a\, c\).........The backslash can be omitted with last line of the replacement text.......

Next is specifying which line(s) of text to work on using line addressing..........Addresses are of three basic formats:
  1. Line numbers - Specify a line simply by using it's line number.
  2. Last line ($) - Matches the last line of text before the end-of-file marker. Think of it as a special line number where you don't actually know how many lines are in the file.
  3. Pattern matching (/regexp/) - Uses regular expression matching to find lines containing the specified pattern. The pattern is enclosed with forward slashes (/pattern/), which is a typical format used by unix commands to indicate regular expression matching. NOTE: If the pattern contains any forward slashes, you will need to escape each occurrence of the forward slash with a backslash(\). However, sed provide a "shortcut" method which allows you to use any single character in place of the enclosing forward slashes, like you can with the substitute command (s@@@). To enable this feature, merely use a backslash immediately before the desired character, such as \@pattern@. This will allow you to use a forward slash in the pattern without the need to escape it with a backslash. Just be sure to NOT use a character found in the pattern itself. ;-)

To specify a range of adjacent lines, a comma (,) is placed between the first address and last address in the range, and can be any mix of line numbers, last line or pattern matching......See the SED manpage for a quick summary on line addresses


So, if you wish to replace a single line of text with three different lines of text, you can state it like this in a shell script:
Code:
Example 1:
sed '24c\
Replacement line one.\
Replacement line two.\
Replacement line three.\
'

Example 2:
sed '$ c \
Replacement line one.\
Replacement line two.\
Replacement line three.\
'

Example 3:
sed '/pattern/              c              \
Replacement line one.\
Replacement line two.\
Replacement line three.\
'

The first example will replace line number 24 with the three lines of text, the second example will replace the last line with the text, and the third example will replace any line containing the matching pattern with the lines of text.........The examples also show it makes no difference if spaces surrounding (before or after) the command are included.......Note the enclosing single quotes in each example.....

TIP: I like to place the final single quote on a line by itself to make it easier to add or remove lines of replacement text, and also include an ending backslash with the last line for consistency.....

You may also write the command on a single line, using the newline escape sequence (\n), like so:
Code:
Example 1:
sed '24cReplacement line one.\nReplacement line two.\nReplacement line three.'

Example 2:
sed '$ c Replacement line one.\nReplacement line two.\nReplacement line three.'

Example 3:
sed '/pattern/              c              Replacement line one.\nReplacement line two.\nReplacement line three.'
While the space is optional surrounding the 'c' command, placing a single space following the command makes for easier reading, and the ending backslash is not used.........

HTH :-)
---thegeekster


Top
 Profile  
 PostPosted: Fri Feb 18, 2011 4:55 pm   

Joined: Thu Feb 10, 2011 5:14 pm
Posts: 2
hi i need some help here i need a script to remove the # from some line
iin a file
sorry missing some more info

example with #
#l1=blaaaa
#l2=yoyojdo
#l3=iuyuio
#l4="something everything here is difrent"
#l5
#l6
#7
#l8
#l9
what i should do is remove the # from line 1,6 and line 9

should look like this when it's done

l1
#l2
#l3
#l4
#l5
l6
#l7
#8
l9
any help to do this from a small script
thanks


Top
 Profile  
 PostPosted: Sat Feb 19, 2011 1:06 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
You are both necroposting a 5 year old post and hijacking somebody else' thread... quite impressive on your second post.
If you have a question create your own post.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Exabot [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP