PDA

View Full Version : UNIX shell scripting help....total noob


suryad
10-31-2005, 08:36 PM
Hey fellas trying my hand at shell scripting. Suppose I want to print out a specific line number from a flatfile...would I need to use awk and sed and so on to accomplish that? I am trying to write a simple shell script but I am hitting a wall. This is my first ever script by the way. Found some good references on the web :thumbsup: and so I can read and interpret shell script examples well. But writing one is something totally different especially since i dont know sed and awk and what tools UNIX already provides to do this sort of thing. Thanks

suryad
10-31-2005, 09:08 PM
#!/bin/sh
# printlnout command - prints out the lines of text as specified in the argument

# this section checks for proper syntax
if [ $# -eq 0 ]
then
echo "Error command arguments missing!"
echo "Usage: printlnout filename start_line end_line"
echo "For eg. printlnout myfile 5 5"
exit 1
fi

# this section checks for existence of the file
if [ -f $1 ]
then
echo "$1 file exists"
else
echo "Error $1 does not exist"
fi

# this section prints out the requested line numbers
if [ $# -eq 3 ] #dunno if i need semi-colons or not
then
if [ -e $1 ]
then
tail +$2 $1 | head -n $3
else
echo "Error reading $1"
exit 2
fi
else
echo "Missing arguments!"
fi


Well fellas that is what I got so far. Learned an awful lot about Shell scripting. I am still in the process of installing Linux on my other comp and I cannot access through SSH a machine with Linux on it since it is being taken down for maintenance. So please let me know if I am doing something stupid or not :D