Nested if in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested if in KSH
# 1  
Old 10-19-2011
Nested if in KSH

Trying to clean up the last little thing in this script which is that the mv always performs no matter whether it is needed or not. It doesn't cause any bugs just prints an unsightly "can't mv source and dest are the same". Obviously I want to get rid of it but having issues getting the nested if to work and google has not been helpful this time.

Below is one of the many variations of nested ifs i have tried from various examples.

thanks in advance
Code:
 for i in `ls -pcr | grep -v [\/] `
 do
                 echo "PRE; $PREMID_DT_TM and Post; $POSTMID_DT_TM"
         if [ -e ${i} ]
         then
             echo "Processing file ${i}"      
             TEMP=$(echo "${i}" | sed -e "s/$PREMID_DT_TM/$POSTMID_DT_TM/")
           if     [[ ${i} -eq ${TEMP}]]
           then
           #
           else
               mv ${i} ${TEMP}
           fi
         fi
 done

# 2  
Old 10-19-2011
Quote:
Originally Posted by Calbrenar
Trying to clean up the last little thing in this script which is that the mv always performs no matter whether it is needed or not. It doesn't cause any bugs just prints an unsightly "can't mv source and dest are the same". Obviously I want to get rid of it but having issues getting the nested if to work and google has not been helpful this time.

Below is one of the many variations of nested ifs i have tried from various examples.

thanks in advance
Code:
 for i in `ls -pcr | grep -v [\/] `
 do
                 echo "PRE; $PREMID_DT_TM and Post; $POSTMID_DT_TM"
         if [ -e ${i} ]
         then
             echo "Processing file ${i}"      
             TEMP=$(echo "${i}" | sed -e "s/$PREMID_DT_TM/$POSTMID_DT_TM/")
           if     [[ ${i} -eq ${TEMP}]]
           then
           #
           else
               mv ${i} ${TEMP}
           fi
         fi
 done

1. You are using a numeric comparision operator (-eq) to compare strings.
2. Why don't you test for inequality instead of equality?
3. You are missing whitespace between your test arguments and the square brackets

Then your test becomes

Code:
if [[ "${i}" != "${TEMP}" ]]
then
   mv ${i} ${TEMP}
fi

There is probably a more efficient way of generating your file list but that's not what your main issue is.
This User Gave Thanks to mobitron For This Post:
# 3  
Old 10-19-2011
I was unable to find a better way to get ls to list by modified times and not include directories. I tried various solutions but the directories were always included.

I will try your suggestion for the if and reply thanks.

---------- Post updated at 11:58 AM ---------- Previous update was at 11:46 AM ----------

Code:
for i in `ls -pcr | grep -v [\/] `
do
                echo "PRE; $PREMID_DT_TM and Post; $POSTMID_DT_TM"
        if [ -e ${i} ]
        then
            echo "Processing file ${i}"      
            TEMP=$(echo "${i}" | sed -e "s/$PREMID_DT_TM/$POSTMID_DT_TM/")
          if [[ "${i}" != "${TEMP}" ]]
                    then
                   mv ${i} ${TEMP}
                    fi
        fi
done

it's saying the then is unexpected.

Also i think I tried testing for inequality first i just kept trying different examples on google trying to get it to work.



Last edited by Calbrenar; 10-19-2011 at 01:08 PM.. Reason: duplicate due to network latency
# 4  
Old 10-19-2011
I'm not sure why it's saying "then" is unexpected. Maybe because you forgot to quote ${i} -- if it's blank for some reason, that becomes [ -e ] instead of [ -e "" ] which is syntactically wrong.

The continue operator is nice for avoiding confusing levels of nesting. It skips the current item and goes back to the top of the loop (or exits if there's no more items). How about:
Code:
for i in ...
do
        [ -f "${i}" ] || continue

        echo "processing "${i}"
        ...
done

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-19-2011
putting quotes around it fixed it. Thanks both of you!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh - Need Help Reducing Nested Loops

Hello, I pulled out some old code from an unfinished project the other day and wanted to stream line it better. I know anything beyond a double loop is usually bad practice, and I came up with some logic for later that would no longer require the first loop in the following code that works: ... (5 Replies)
Discussion started by: Azrael
5 Replies

2. Shell Programming and Scripting

Nested if else

Hi, i m trying to create script which logic is like below. if ; then x=`cat /tmp/testoutput.log | grep STOP | wc -l` y=`cat /tmp/testoutput.log | grep RUN | wc -l` if ; then echo "process stop" if ; then echo "process running " else echo "file not found" fi ----------------... (2 Replies)
Discussion started by: tapia
2 Replies

3. Shell Programming and Scripting

Nested case

Hi there, I have nested case in my script. I am asking user, want to continue? if user press y/Y then my inner case should continue, rather than that my code start from beginning. I would like to continue my inner case until user press n or N. Is any one tell me how can I do? Thanking You,... (2 Replies)
Discussion started by: kasparov
2 Replies

4. Shell Programming and Scripting

KSH nested loops?

KSH isn't my strong suit but it's what my company has to offer. I've got a script with two nested loops, a FOR and UNTIL, and that works fine. When I add a CASE into the mix I end up getting "Unexpected 'done' at line xx" errors. Any suggestions on this? for divi in at ce ci cm co de di fl... (9 Replies)
Discussion started by: mrice
9 Replies

5. Shell Programming and Scripting

Nested If condition

Hi I have a requirement to create a 2 folder based on there existance if then cd $var_name if then cd $var_name3 mv -fi *.* $var_TargetPath/$var_name/$var_name3 else mkdir -p "$var_name3" chmod 755 "$var_name3" mv -fi *.* $var_TargetPath/$var_name/$var_name3 else mkdir... (7 Replies)
Discussion started by: magesh_bala
7 Replies

6. Shell Programming and Scripting

Nested while loops (ksh scripting)

You can use one while inside another? I made the following script (without really knowing if I can use two while) to get 3 numbers different from each other at random: num1=$(( $RANDOM % 10 )) num2=$num1 while do num2=$(( $RANDOM % 10 )) done num3=$num1 while do while do... (1 Reply)
Discussion started by: ale.dle
1 Replies

7. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

8. Shell Programming and Scripting

nested variables

Is there any way to do variable nesting using sh? For example: example_1="a test string" example_2="another test" example_3="etc..." i=2 echo ${example_$i} The shell reports: sh: ${example_$i}: bad substitution If not, maybe someone could suggest another method. Thanks in... (3 Replies)
Discussion started by: kevinl33
3 Replies

9. Shell Programming and Scripting

Nested calls

Hi all, I have two questions: 1. I have a script (call it A) that call another script (call it B) which in turn call another script (call it C). The strange thing is that when script C hangs script A exists (i.e., will not appear when I call ps command to list all the running processes). Is my... (6 Replies)
Discussion started by: omran
6 Replies
Login or Register to Ask a Question