Bash script to open, read, and write then save....

Posted by Alex Vo on Server Fault See other posts from Server Fault or by Alex Vo
Published on 2011-01-12T17:03:34Z Indexed on 2011/01/12 17:55 UTC
Read the original article Hit count: 183

Filed under:

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

© Server Fault or respective owner

Related posts about bash-scripting