Sponsored Content
Top Forums Shell Programming and Scripting Another IFS post, [ bourne SH only ] ? Post 302375971 by gcampton on Monday 30th of November 2009 08:52:58 AM
Old 11-30-2009
Quote:
Originally Posted by radoulov
Well, it depends. The following syntax affects the IFS only for the read command inside the while loop, so you don't need to worry/restore the previous value of IFS for the statements after the loop.
I understand that, but that is only in the case of
Code:
 while IFS=: read ...

what I am saying is I have been told to do it differently in the format of:
Code:
OLDIFS=$IFS
IFS=":\n"

I am unsure why exactly I must use OLDIFS, but I assume it's a problem with sh? so far all tests I have done using IFS in while loop in sh, ash, bash, ksh all seem to work fine... So I really have no idea why my tutor seems to think otherwise, would have been helpful if I asked him to expand on this..


Quote:
Originally Posted by radoulov
Code:
while IFS= ...

Sometimes it's convenient to force a sub-shell execution to avoid affecting the current shell environment, but it depends on what exactly you' re trying to achieve.
I think the basic logic behind it is, 'sh' doesn't handle subshells very well, while bash/ksh etc do handle subshells fine. But the tutor is trying to teach us portable languages.

Quote:
Originally Posted by radoulov
Please post the exact command(s)/output (just copy/paste) you' re getting.
Like I have already mentioned my problems using OLDIFS variable is that my constant variables that i use to echo statements to standard output are missing the n's

Code:
 
 ew record added to database
 ew record added to database
 ew record added to database

however this is NOT for all my variables I have just checked over and tested a few of them, and they work, however addding, and failure to delete is leaving out n's, which seems wierd to me. You would think they all would not work, or they all would work. Not randomly willy nilly.
As you can see from my first post, I showed the while loop calling functions, so in the case of using OLDIFS/NEWIFS I assume those functions and variables called from there also use it. which is why i'm missing some "n's"

Code:
# arg errors
USAGE="usage: ./a3 -d dataBaseFilename -o operationsFilename"
ERRNOARG="error:Invalid command line not enough arguments"
ERRNOFILE="error:No operations file supplied"
ERRNOREAD="error:can't read"
ERROPEMPTY="error:Operations file empty"
ERRILLOPT="illegal options:"

# opt errors
ERRNOADD="unable to add"
ADDFORMAT="add:product:brand:isle"
ERRNODEL="not deleted does not exist"
DELFORMAT="delete:product:brand"
ERRNOBRAND="brand does not exist"
ERRNOPROD="does not exist"
ERROPERATE="unknown operation"
ERREXISTS="product already exists"

# common output
RECADDED="new record added to database"
DATAEMPTY="database is empty - nothing to display"
RECDELETE=":deleted"

readonly USAGE ERRNOARG ERRNOFILE ERRNOREAD ERROPEMPTY
readonly ERRILLOPT ERRNOADD ERRNODEL ERRNOBRAND ERRNOPROD
readonly ERROPERATE ERREXISTS RECADDED DATAEMPTY RECDELETE

DATAEMPTY seems to work, as does USAGE...
RECADDED does not

thanks for the help, if you need to see more of my code, eg where I am calling the variables from just ask Smilie

Last edited by gcampton; 11-30-2009 at 09:58 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

the IFS variable

Hi all, Ok os heres my situation. I have created a database style program that stores a persons info (name,address,phone number etc.) in a file ("database"). after i read in all the values above, i assign them to a line variable: line="$name^$address^$phonenum" >> phonebuk as you can see... (1 Reply)
Discussion started by: djt0506
1 Replies

2. UNIX for Dummies Questions & Answers

IFS variable

How can I set the value for IFS variable (2 Replies)
Discussion started by: mahabunta
2 Replies

3. UNIX for Dummies Questions & Answers

Help on IFS command!

Hi! I am working in korn shell. I want to reset the dimiliter for the set command to "|" but instead of a command prompt return I am getting something as below After issuing the command I am getting this....as if the shell is expecting something else. Can anybody suggest what's the problem. ... (2 Replies)
Discussion started by: udiptya
2 Replies

4. Shell Programming and Scripting

regarding IFS=

hi i am a learner can some explain "export IFS=$(echo "\n\t\a")" i am not able to understand the functionality please help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies

5. Shell Programming and Scripting

read and IFS

Hi, This is out of curiosity: I wanted to extract year, month and date from a variable, and thought that combining read and IFS would help, but this doesn't work: echo "2010 10 12" | read y m d I could extract the parts of the date when separated by a -, and setting IFS in a subshell: ... (3 Replies)
Discussion started by: raphinou
3 Replies

6. Shell Programming and Scripting

How to use IFS in this scenario?

Given the scenario like this, if at all if have to use IFS on the below given example, how it should be used. IFS=/ eg: /xyz/123/348/file1 I want to use the last slash /file1 . So can anyone, suggest me how to pick the last "/" as a IFS. (4 Replies)
Discussion started by: raghunsi
4 Replies

7. Shell Programming and Scripting

While loop and IFS?

Hi, while ; do echo "Please enter " read enter yyyy=${enter:0:4} mm=${enter:5:2} dd=${enter:8:2} result=`validateDate $yyyy $mm $dd` When does the loop keeping repeating till?? till 1 is equal to 1? what does this mean "${enter:0:4}" .The 0 and 4 part?? ... (3 Replies)
Discussion started by: sid22
3 Replies

8. Shell Programming and Scripting

Nested ifs

hi I keep getting an error with this nested if statement and am getting the error unexpected end of file, can anyone help me as to why this wont execute? #!/bin/bash #script to check wether the -i -v statements run correctly removeFile () { mv $1 $HOME/deleted }... (3 Replies)
Discussion started by: somersetdan
3 Replies

9. Shell Programming and Scripting

Bash IFS

I am using bash and resetting IFS as below when reading the command line arguments. I do this so I can call my script as in Ex1. Ex1: ./synt2d-ray3dmod.bash --xsrc=12/20/30 This allows me to split both sides so that when I do "shift" I can get 12/20/30 What I do not understand is... (21 Replies)
Discussion started by: kristinu
21 Replies

10. Shell Programming and Scripting

Not able to understand IFS

Hi , i am in my initial learning phase of unix. i was going thru the function part. below is the example which was there but i am not able to understand logic and the use of IFS(internal field separator) lspath() { OLDIFS="$IFS" IFS=: for DIR in $PATH ; do echo $DIR ; done IFS="$OLDIFS"... (8 Replies)
Discussion started by: scriptor
8 Replies
All times are GMT -4. The time now is 03:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy