changing to a different file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting changing to a different file
# 1  
Old 04-10-2006
get user to enter a filename and add word to that file instead...

Hi,

At the moment, I have inserted an actual filename to conduct searches through it etc... Instead, I want the user to choose a filename and then do the searches against this file, instead of already having specified the file in the code. How would I do this? I tried doing the following, but it says cannot find file or directory, but the file does exist.

echo "enter file"
read file
echo "enter word to add"
read word
echo "$word" >> "$file"
------------------------

Thanks for your help.

Last edited by kev269; 04-10-2006 at 07:16 PM..
# 2  
Old 04-11-2006
That code to work fine for me. Try enabling the debug mode i.e. set -x to enable and set +x to disable.

Code:
set -x
echo "enter file"
read file
echo "enter word to add"
read word
echo "$word" >> "$file"
set +x

# 3  
Old 04-11-2006
hi
first chk it out in debug mode.

otherwise use complete path with filename instead of only filename. use "pwd"

hope so it works.


regards,
swapneel
# 4  
Old 04-11-2006
thanks, i just have another question. I have two files - 'menu' and 'addword'. The user has to run 'sh menu' and then a menu appears asking the user to enter a filename and to press 0 from the menu to add a word to the end of the text in the entered filename.

Now, i entered a filename at the beginning of the menu when it asked me to and this was stored in $file as you can see from the code, but when i choose to run addword by pressing menu option 0, i entered a word to add, but it would not add to $file. It says ambiguous redirect. It is as though the filename that i entered at the menu option has been forgotten. Can it not stay in memory for more than one file? thanks.

----------------------------
Menu
----------------------------
echo "enter filename of text"
read file
echo "0: Add word"
read entry
case $entry in
0) sh addword;;
esac
echo
done

--------------------------------
addword
--------------------------------
echo "Enter the new line to add:"
read newline
echo "$newline" >> $file
# 5  
Old 04-11-2006
you are getting the file variable value in one shell script and using it in another shell script... this is not valid

You should either do export of file variable before calling addword script or pass file variable as a argument to addword script...
# 6  
Old 04-11-2006
hi

u need to pass file name to new script.
see this thread

https://www.unix.com/shell-programming-and-scripting/26603-passing-values-shell-script.html
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing file permissions of a file created by another user

Hi, I have used expdp for datapump. The .dmp file is created by the "oracle" user. my requirement is to make a zipped file of this .dmp file. What i am trying to do is change the permissions of this .dmp file from 0640 to 0644 and then do a gzip and zip it. Is there any way i can change... (3 Replies)
Discussion started by: qwertyu
3 Replies

2. Homework & Coursework Questions

Changing a file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Make “hardlink” readable, writeable, executable by you, but not readable, writeable, executable by anyone else.... (4 Replies)
Discussion started by: lilbo4231
4 Replies

3. Shell Programming and Scripting

changing a file when the inode modified time of the other file changes

i have a requirement where i needed to change variable values in a properties file(first file) whenever there is change to Release details file(second file). My question is do i have to create a daemon process that always checks the modified time/inode change of the second file and then change the... (1 Reply)
Discussion started by: saikiran_1984
1 Replies

4. UNIX for Dummies Questions & Answers

Changing file content based on file header

Hi, I have several text files each containing some data as shown below: File1.txt >DataHeader Data... Data... File2.txt >DataHeader Data... Data... etc. What I want is to change the 'DataHeader' based on the file name. So the output should look like: File1.txt >File1 ... (1 Reply)
Discussion started by: Fahmida
1 Replies

5. Shell Programming and Scripting

Changing Name of File

Write a shell program to replace the starting alphabet "a" with "k" for the file names in the current directory. Note : Change only the file names starting with "a". Rest of the file names remains unchanged. Homework. Replies deleted. Closed. (0 Replies)
Discussion started by: shashwat2691
0 Replies

6. Shell Programming and Scripting

changing csv file contents to file of rows

i have a file a.txt contents as 1,2,3,4,......etc...in a single line, i want to write to another file in rows as 1 2 3 4 5 can u help? i do not know the length of a.txt (4 Replies)
Discussion started by: pravfraz
4 Replies

7. Shell Programming and Scripting

changing line on a file

Hi, I want to change on a file this line: vif = into: vif = But there are some conditionals: 1. last part '] of the original line after 00:16:3E:CE:23:14' ] may vary to 00:16:3E:CE:23:14' ] or 00:16:3E:CE:23:14 ' ] 2. The second mac adrress of the line I need,... (4 Replies)
Discussion started by: iga3725
4 Replies

8. UNIX for Dummies Questions & Answers

Changing old file name to new one if the old file name satisfied certain condition

Hi guys, I desperately need your help on my problem:- I got a folder in my directory that has many files in it. Most of the file's name is starting with certain prefix. Eg: ABC, CDE, EFG, etc. I only want to change file name that is starting from certain prefix and the rest I want them to... (1 Reply)
Discussion started by: balzzz
1 Replies

9. UNIX for Dummies Questions & Answers

Changing the File Name

Dear all, I want to change all the File name in a folder from File1.txt to File1YYYYMMDD.txt where YYYYMMDD is the date of the file is renamed. I have used the following scripts:- ******************************************************** #!/bin/ksh A=`date +"%d%m%Y" for in * ... (2 Replies)
Discussion started by: balzzz
2 Replies

10. Shell Programming and Scripting

Changing userID and Changing group and GID

Hello, I want to write a ksh script about changing UID and changing group with GID. There are multiple servers i want to perform that job. linux1 linux2 linux3 linux4 linux5 ...... . . . . . 1.) How can i enter "password" in script rather asking me? I was trying this... ssh... (2 Replies)
Discussion started by: deal732
2 Replies
Login or Register to Ask a Question