Question on the use of while [-z]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question on the use of while [-z]
# 1  
Old 02-12-2015
Lightbulb Question on the use of while [-z]

Hi,
I am new in unix. I found one while command as while [ ! -z "$SRC_FILE" ].
It would be really help if you could let me know what is the use of "-z" and what is difference between "-n" and "-z". Thanks for your help!!Smilie

Last edited by Don Cragun; 02-12-2015 at 06:12 PM.. Reason: Add ICODE tags.
# 2  
Old 02-12-2015
Please use code tags as required by forum rules!

Reading man bash (or whatever shell you use) would help. Paragraph on CONDITIONAL EXPRESSIONS:
Quote:
-z string
True if the length of string is zero.
string
-n string
True if the length of string is non-zero.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-12-2015
Thanks RudiC!!
Code:
export SRC_FILE = ls -at xxx_yyy_* |tail -n 1
   while [ ! -z "$SRC_FILE" ]
      do
            mv $SRC_FILE `date +%Y%m%d_%H%M%S`_xxx_yyy.txt
            sleep 10

           SRC_FILE=`ls -at xxx_yyy_* |tail -n 1`
      done

in this case what will happen if there are five files with name 'xxx_yyy_*'?
will it rename all or only the first one?
I think in our case, only first file gets renamed and others are untouched.
thanks for help.
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for multi-line input, output, and code segment samples.

Last edited by Don Cragun; 02-12-2015 at 06:13 PM.. Reason: Change ICODE tags to CODE tags.
# 4  
Old 02-12-2015
This is an overcomplicated approach to moving n files. You repeatedly list ALL files and then tail only the last - which is changing in every iteration as the former one has been moved. Some comments:
- no need to export - var is only used locally
- no need to use the -at options to ls
- the file_to_process.txt is not used anywhere
- the date stamp is evaluated in every iteration and will be changing between loops. Oh, I see, it's used to differentiate between files. Is that wise?
Anyhow, how about
Code:
ls xxx_yyy_* |
while read FN
  do DATESTAMP=$(date...)
     mv "$FN" $ARCHIVE_PATH/$DATESTAMP_xxx_yyy.txt
  done

This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-12-2015
Quote:
Originally Posted by SantanuBiswas
Thanks RudiC!!
Code:
export SRC_FILE = ls -at xxx_yyy_* |tail -n 1
   while [ ! -z "$SRC_FILE" ]
      do
            mv $SRC_FILE `date +%Y%m%d_%H%M%S`_xxx_yyy.txt
            sleep 10

           SRC_FILE=`ls -at xxx_yyy_* |tail -n 1`
      done

in this case what will happen if there are five files with name 'xxx_yyy_*'?
will it rename all or only the first one?
I think in our case, only first file gets renamed and others are untouched.
thanks for help.
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for multi-line input, output, and code segment samples.
Here is a slightly different take on what RudiC has already said about the above script...

There are several problems with this script (including, but not necessarily limited to):
  1. The export command produces no output, so piping its output to tail doesn't do anything except make your script run slower.
  2. There are no subshells or commands started by this script that will care whether or not a variable named SRC_FILE is present in their environment, so the export command is not needed.
  3. There cannot be a space between a variable and the = or between the = and the value being assigned in a shell variable assignment statement (either on its own or in an export command).
  4. Typing a command after an = in an assignment statement does not execute that command. You need to use command substitution to do that.
  5. For filenames starting with "x", the ls -a option is a no-op.
  6. Why re-evaluate the list of files to be processed every time you process a file? Why not just get a list of files and process them one by one?
Perhaps, something like this would better suite your needs:
Code:
for SRC_FILE in xxx_yyy_*
do	if [ -f "$SRC_FILE" ]
	then	mv "$SRC_FILE" "$(date '+%Y%m%d_%H%M%S')_xxx_yyy.txt"
		sleep 1
	fi
done

The difference between this script and RudiC's suggestion is that the above script will run a little faster (since it doesn't execute the ls command) but it won't generate a diagnostic message if there are no files with names starting with xxx_yyy_ when you start the script (although one could be added as an else clause in the if statement. If there are files matching your pattern that are not regular files, this script will ignore them while RudiC's script will move them. And, since there is no delay between moves in RudiC's script, files could be overwritten if two or more files are moved within the same wall clock second. (The sleep 1 in the loop in my code avoids that problem. I don't see any need for the 10 second delay you had in your loop.)

And, finally to answer your question: "what will happen if there are five files with name 'xxx_yyy_*'?" I would expect a syntax error from the export command that will leave SRC_FILE undefined. So the expansion of $SRC_FILE in the while loop will be a zero length string and the commands in the while loop will never be executed unless your script inherits a value for that variable in its environment. If a value is inherited in the environment, zero or more files not matching your pattern could be moved or could cause a diagnostic message to be printed if mv can't move a file or several files (since you didn't quote the expansion in the mv command) named by that variable, and then the script would go on to do what you want because you reset that variable at the end of the loop each time you go through the loop.

Last edited by Don Cragun; 02-12-2015 at 08:20 PM.. Reason: Fix typo s/executed/moved/
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. AIX

df question

Hi, Can anyone please explain a little about df command. I have following question: Following example is showing % used as 4 where as total free blocks are 15.46 out of 16.00 MB blocks. df -m /test Filesystem MBblocks Free %Used Iused %Iused ... (5 Replies)
Discussion started by: itsabhi9
5 Replies

2. Hardware

question

How to add 3 moniters to a pc set up? (2 Replies)
Discussion started by: clicstic
2 Replies

3. Shell Programming and Scripting

question about wc

Hey my friend was asking me if i knew a way to cout how many different words in a file. I told him no not off hand, but i was thinking about it, and i started to wonder also. I imagine this is probably pretty simple im just missing something, I keep confusing my self with how you would compair and... (16 Replies)
Discussion started by: yodadbl07
16 Replies

4. UNIX for Dummies Questions & Answers

Question

hallo, ik heb hier een vraagje. hoeveel gebruikers kunnen er op 1 unix systeem. hopelijk antwoorden golle nu want ik moet da vinde voor school en die leerkracht zaagt. :p groetjes eu wacht wa was mijne nick ah ja vraagje groetjes vraagje ik kan geen engels dus antwoord liever in het... (1 Reply)
Discussion started by: vraagje
1 Replies

5. UNIX for Dummies Questions & Answers

mv question

Hello if I like to move file from defined directories system to new directory that not contained any directories system structure . But I like to create the same file system structure as source directory for example : I have 2 directories: foo1 and foo2 foo1 have directories and foo2 have... (2 Replies)
Discussion started by: umen
2 Replies

6. Solaris

vi question

Im trying to edit a 113 meg file in VI and i get the error TMP FILE TOO LARGE. Does someone know how to get around this? Thanks! (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies

7. Programming

Yet Another Question

Now that I have getch() to work, I have yet another problem. BTW, thank you for answering these questions, I do ask a lot, only because I am eager to know, what is a board used for anyways :) Ok, he's the problem... #include iostream.h #include conio.h int main() { char movement; ... (2 Replies)
Discussion started by: mbolthouse
2 Replies
Login or Register to Ask a Question