LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-12-2010, 10:02 PM   #1
balakodoth
Member
 
Registered: Dec 2010
Posts: 40

Rep: Reputation: 0
how to ovewrite a file in shell script


I want to overite a file using shell script.

i need to overite the file withe value of 2nd argument.

echo $2 > /home/dir/file.txt whether this will work? or what is the correct syntax?
 
Old 12-13-2010, 01:56 AM   #2
murugesan
Member
 
Registered: May 2003
Posts: 155

Rep: Reputation: 28
A sample script using ">>" to append the file:


Code:
#!/bin/ksh
echo Enter existing file name:
while [ 1 ]
do
                read
                FILENAME=$REPLY
                if [ ":$FILENAME:" != "::" ] &&  [ -f $FILENAME ]
                then
                                echo if
                                while [ ":$FILENAME:" != "::" ]
                                do
                                        echo Enter any word to append it with the file $FILENAME
                                        read
                                        if [ ":$REPLY:" != "::" ]
                                        then
                                                echo $REPLY >> $FILENAME
                                                echo The following file is appended with the word $REPLY
                                                echo $FILENAME
                                                break
                                        else
                                                REPLY=""
                                        fi
                                done
                                break
                elif [ ":$FILENAME:" != "::" ] && [ -d $FILENAME ]
                then
                        echo The following name you entered is a directory
                        /bin/ls -ld $FILENAME
                        echo Enter existing file name:
                elif [ ":$FILENAME:" = "::" ]
                then
                        echo Please enter existing file name:
                else
                        echo The following file is not found.
                        echo $FILENAME
                        echo Enter existing file name:
                fi
done

http://geocities.ws/murugesan/techni...ll_script.html
 
1 members found this post helpful.
Old 12-13-2010, 02:14 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by balakodoth View Post
... whether this will work? or what is the correct syntax?
Looks good. Any whitespace characters (space, tab, newline) in $2 will be replaced by single spaces unless you change it to "$2". You could try it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script that adds users name to a file when the file is modified netting4u Linux - General 1 03-30-2010 04:09 AM
Need a shell script to turn a lined file into a comma seperated file Thaidog Linux - General 8 03-08-2008 07:19 PM
cannot execute script file from shell shahrahulb Linux - General 4 03-04-2008 07:02 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
File Parsing using a Shell Script yasir15 Programming 5 08-22-2003 12:17 PM


All times are GMT -5. The time now is 06:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration