Sunday, November 1, 2009

Shell Script to Create a File with Specified Number of Lines


#!/bin/bash
#
# SCRIPT: bigfile.sh
# PURPOSE: This script is used to create a text file that
# has a specified number of lines that is specified
# on the command line.
#


usage()
{

echo "............USAGE ERROR............"
echo "USAGE: $0 <number_of_lines_to_create>"
}

# Argument Checking

if [ $# -ne 1 ] # Looking for exactly one parameter
then
usage # Usage error was made
exit 1 # Exit on a usage error
fi

# Define files and variables here

LINE_LENGTH=80 # Number of characters per line
OUT_FILE=bigfile.$$ # New file to create
>$OUT_FILE # Initialize to a zero-sized file
TOTAL_LINES=$1 # Total number of lines to create
LINE_COUNT=0 # Character counter
CHAR=X # Character to write to the file

# Beginning of Main

while ((LINE_COUNT < TOTAL_LINES))
do
CHAR_COUNT=0 # Initialize the CHAR_COUNT to zero on every new line

while ((CHAR_COUNT < LINE_LENGTH)) # Each line is fixed length
do
echo -e "$CHAR\c" >> $OUT_FILE # Echo a single character
let CHAR_COUNT++ # Increment the character counter
done

let LINE_COUNT++
echo>>$OUT_FILE # Give a newline character
done

7 comments:

  1. your blog is rocking man. I practiced many scripts form your blog. It's really helpful.
    Thank for sharing your knowledge.

    K Pallavi

    ReplyDelete
  2. There's no need to repeat the inner loop for every line:

    LINE=
    while [ $(( CHAR_COUNT += 1 )) -le $LINE_LENGTH ]
    do
    LINE=$LINE$CHAR
    done

    while [ $(( LINE_COUNT += 1 )) -le $TOTAL_LINES ]
    do
    echo "$LINE"
    done > "$OUT_FILE"



    ReplyDelete
  3. i have a problem with updating one of the fields in my file....i want the field to be updated everyday by adding one when the employee is absent

    thank you in advance
    cadinho

    ReplyDelete
  4. This article great helped him and tell us how to create a shell script file and how to save data thanks for share it biosketch format .

    ReplyDelete
  5. Can anyone clear my concept behind loops.. Taking variables.. I am beginner.. Reply asap

    ReplyDelete
  6. It’s very excellent information

    ReplyDelete