Sponsored Content
Top Forums Shell Programming and Scripting Another IFS post, [ bourne SH only ] ? Post 302375938 by gcampton on Monday 30th of November 2009 07:17:34 AM
Old 11-30-2009
Another IFS post, [ bourne SH only ] ?

I know there's a large number of posts on this, I have read a number of them. But unable to find specifics.

Scrutinizer showed me a nice trick using IFS in a while loop in a BASH script, however when I went to hand in my work my tutor informed me that the criteria specifically states SHell script only, he also said I was "unable to do that" in regard to IFS. Looking through his examples in notes, it's all stuck in variable OLDIFS as per usual. My only problem is that I'm using that to parse a file in a while loop which then proceeds to call a number of functions.

So I am uncertain where to change my variable back to the old ifs, as so far I have tried different parts of my loop at the end of loop (which happens to be end of script) and the only other way I can see this working is if I change the variable at the start of each function. So I would have 8 declarations of IFS=$OLDIFS. And there's no guarantee that once i change it back to the OLDIFS in one function, once that function is complete and it enters back into my while loop that it will continue using the new IFS, so I would have to have another 8 declarations of OLDIFS=$IFS; IFS=":\n" at the end of each function as well...

the IFS=":\n" is only messing with my n's in my variable output eg: my add successful statement:
ADDEDPROD="new product added sucessfully"
comes out as " ew product added successfully"
and error for delete.
"unable to delete product, does not exist in database"
"u able to delete product, does ot exist in database"
and a few other error/output variables, everything else seems to work fine eg. Unix tools...

would be good if there was a way around this, anyone know?
or will i just have to change IFS back to OLDIFS at the start and end of every function?

another cygwin only related question for those that use it is, is there anyway to specify to use a SH script? because I tried with the IFS in while loop running in sh by just using
Code:
 sh ./script

however this seemed to work fine, so either the cygwin shell script isn't close enough to bourne shell, or the above line didn't open a new 'sh' shell or my teacher is misinformed. (i doubt that last part, but he is still human... I think Smilie)
Code:
tr '[:lower:]' '[:upper:]' < $file |
while IFS=: read cmd arg1 arg2 arg3; do
    case $cmd in
        ADD)  add "$arg1" "$arg2" "$arg3"
            ;;
        DELETE) delete "$arg1" "$arg2"
            ;;
        DISPLAYALL) displayall 
            ;;
        BRAND) fbrand "$arg1" 
            ;;
        PRODUCT) fproduct "$arg1"
            ;;
        *) echo "operation unknown"
            ;;
    esac
done
# this seems to be the only declaration that works in either shell

Code:
#this one gives me errors, but is apprently the right way to do it for bourne shell.
OLDIFS=$IFS
IFS=":\n"
tr '[:lower:]' '[:upper:]' < $file |
while read cmd arg1 arg2 arg3
do
    ...
    .......
    ...
done

 

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
CHECKBASHISMS(1)					      General Commands Manual						  CHECKBASHISMS(1)

NAME
checkbashisms - check for bashisms in /bin/sh scripts SYNOPSIS
checkbashisms script ... checkbashisms --help|--version DESCRIPTION
checkbashisms, based on one of the checks from the lintian system, performs basic checks on /bin/sh shell scripts for the possible presence of bashisms. It takes the names of the shell scripts on the command line, and outputs warnings if possible bashisms are detected. Note that the definition of a bashism in this context roughly equates to "a shell feature that is not required to be supported by POSIX"; this means that some issues flagged may be permitted under optional sections of POSIX, such as XSI or User Portability. In cases where POSIX and Debian Policy disagree, checkbashisms by default allows extensions permitted by Policy but may also provide options for stricter checking. OPTIONS
--help, -h Show a summary of options. --newline, -n Check for "echo -n" usage (non POSIX but required by Debian Policy 10.4.) --posix, -p Check for issues which are non POSIX but required to be supported by Debian Policy 10.4 (implies -n). --force, -f Force each script to be checked, even if it would normally not be (for instance, it has a bash or non POSIX shell shebang or appears to be a shell wrapper). --extra, -x Highlight lines which, whilst they do not contain bashisms, may be useful in determining whether a particular issue is a false posi- tive which may be ignored. For example, the use of "$BASH_ENV" may be preceded by checking whether "$BASH" is set. --version, -v Show version and copyright information. EXIT VALUES
The exit value will be 0 if no possible bashisms or other problems were detected. Otherwise it will be the sum of the following error val- ues: 1 A possible bashism was detected. 2 A file was skipped for some reason, for example, because it was unreadable or not found. The warning message will give details. SEE ALSO
lintian(1). AUTHOR
checkbashisms was originally written as a shell script by Yann Dirson <dirson@debian.org> and rewritten in Perl with many more features by Julian Gilbey <jdg@debian.org>. DEBIAN
Debian Utilities CHECKBASHISMS(1)
All times are GMT -4. The time now is 04:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy