1

I'm new on this bash script thing. Can you show me some example on writing Bash script. I want to write a script that can read from a filename and save it to a variable; increment the value of the variable and write that variable back to the file and save it. This is what I have started and stuck on it so far.

#!/bin/bash
# if file exist

#echo "Testing \ "$1""
if [ -f "$1" ]; then
 echo "$1 does exist"
else
 echo "$1 does not exist!" 
 echo "Creating $1"
 touch $1

 echo "This is test" > $1
 exit 1
fi

#echo "Testing \ "$2""
if [ "$2" == "" ]; then
 echo "Enter the filename"
elif [ -f "$2" ]; then
 echo "$2 Fille does exist" 
else
 echo "$2 File doesn't exist"
 echo "Creating $2"
 touch $2
exit 1
fi

counter=1
echo -n "Enter a file name : "
read file

if  [ ! -f $file ]
then
    echo "$file not a file!"
    exit 1
fi
1
  • 1
    Based on your code "so far" I have no idea what you're trying to do. It looks like you're running the script with two filenames on the command line ($1 and $2) then asking for a third filename in the script read file... which file are you trying to read/increment/write?
    – DerfK
    Jan 12, 2011 at 17:18

1 Answer 1

1

Here is your script with some modifications.

#!/bin/bash
# if file exist

#echo "Testing \ "$1""
if [ "$1" == "" ]
then
    read -r -p "Enter the filename" file1
else
    file1=$1
fi

if [ -f "$file1" ]
then
    echo "$file1 does exist"
else
     echo "$file1 does not exist!" 
     echo "Creating $file1"
     echo "1" > "$file1"
     exit 1
fi

#echo "Testing \ "$2""
if [ "$2" == "" ]
then
    read -r -p "Enter the filename" file2
else
    file1=$2
fi

if [ -f "$file2" ]
then
    echo "$file2 does exist"
else
     echo "$file2 does not exist!" 
     echo "Creating $file2"
     echo "1" > "$file2"
     exit 1
fi

if [ "$3" == "" ]
then
    read -r -p "Enter the filename" file3
else
    file3=$3
fi

# the following assumes that the data from the file is an integer
# and that it consists of only one line containing one value
# similar techniques can be used to do something much more powerful
data1=$(<"$file1")
data2=$(<"$file2")
# it's usually a good idea to validate data, but I have not included any validation
((data3 = data1 + data2))
echo "$data3" > "$file3"    # overwrite the previous contents of the file with the new value

As long as neither file1 or file2 changes, the contents of file3 will always be the same on repeated runnings of the script above. If file1 or file2 don't exist, a "1" is written to them as a default value.

Here is an improved version of the script using functions:

#!/bin/bash
checkarg () {
    local filename=$1
    if [ "$filename" == "" ]
    then
        read -r -p "Enter the filename" filename
    fi
    echo "$filename"
}

checkfile () {
    local filename=$1
    if [ -f "$filename" ]; then
        echo "$filename does exist"
    else
         echo "$filename does not exist!" 
         echo "Creating $filename"
         echo "1" > "$filename"
         # you could remove this exit if you want the script to continue
         # with newly created files instead of exiting
         exit 1
    fi
}

file1=$(checkarg "$1")
checkfile "$file1"

file2=$(checkarg "$2")
checkfile "$file2"

file3=$(checkarg "$3")

data1=$(< "$file1")
data2=$(< "$file2")
((data3 = data1 + data2))
echo "$data3" > "$file3"
6
  • i want to thank your first for your help...i'm trying to read from $1 and increment from $2 and write it to a destination which is $3 and save it. i think my script is bad. i didn't get it to do what i want it to....i don't know if this is possible..can you help...thank you again..
    – Alex Vo
    Jan 13, 2011 at 21:34
  • A system administrator is the only one who is likely to use ksh for anything serious - not to mention counting within a file. This question is relevant.
    – Mei
    Jan 13, 2011 at 22:27
  • 1
    @David: The OP is using Bash. There are others who use Bash and ksh for serious purposes all the time. Developers are one example. I have removed my objection from my answer in any case. Jan 14, 2011 at 1:32
  • @Alex: See my edited answer which hopefully fulfills that requirement. Jan 14, 2011 at 1:33
  • Dennis..sorry for a late reply. thank Dennis and all of you guys. your comment has a great help for me.....
    – Alex Vo
    Jan 21, 2011 at 21:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .