Question using 2 variables in a "for" statement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Question using 2 variables in a "for" statement
# 1  
Old 07-09-2015
Question using 2 variables in a "for" statement

Good Morning,
I typically run a batch of commands, from the command line, to process server operating statistics. That would look like this:

Code:
[ es402 #time for PB in 06 07 08 09 ] (these are days of the month)
[do ]
[./out402_3.ksh 07 $PB 15; mv *1507${PB}* july_2015; rm temp*; ls ]
[ done ]

In this instance I am processing a directory of file for July 6th 7th etc.
Those files would have names similar to:
Code:
SYSCHECK-es402-150707_032100.log (date and times)

On occasion I need to process files from two months, say June 29 and 30 as well as July 1 and 2.

I'm looking for a way to pass a variable for both Month and Day from the command line, rather than entering the command line twice, once for June and then once again for July.

If someone has an idea, I would be most appreciative to hear.
Thank you!

Last edited by Scrutinizer; 07-09-2015 at 01:13 PM.. Reason: code tags
# 2  
Old 07-09-2015
How about nested for loop?

E.g.
Code:
for day in 06 07 08 09
do
        for month in 06 07
        do
                <command> $month $day 15
        done
done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 07-09-2015
I don't think that would work - where's 1. and 2. July?

---------- Post updated at 19:35 ---------- Previous update was at 19:33 ----------

What OS, shell, and date (command) version do you have?

---------- Post updated at 19:39 ---------- Previous update was at 19:35 ----------

How about this bashism:
Code:
for D in {29..30}.06. {01..06}.07.; do echo $D; done
29.06.
30.06.
01.07.
02.07.
03.07.
04.07.
05.07.
06.07.

# 4  
Old 07-09-2015
How about:
Code:
for MD in "06 29" "06 30" "07 01" "07 02"
do
   set $MD
   M=$1 D=$2
   echo "Month: $M  Day: $D"
done


or:
Code:
for MD in "06 29" "06 30" "07 01" "07 02"
do
   read M D << EOF
   $MD
EOF
   echo "Month: $M  Day: $D"
done


or bash / ksh93 /zsh
Code:
for MD in "06 29" "06 30" "07 01" "07 02"
do
   read M D <<< "$MD"
   echo "Month: $M  Day: $D"
done


Last edited by Scrutinizer; 07-09-2015 at 09:15 PM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 07-10-2015
Hi RudiC,
I'm using Tru64 version 5.1B and a korn shell.
Thanks for posting, I need to tryout some of these ideas.

---------- Post updated at 08:01 AM ---------- Previous update was at 07:56 AM ----------

Scrutinizer, this looks promising. Also, it looks like I can even test this, now, while I only have July data to work with.
Thank you.
This is a good trick to put in my bag, extracting more than one value from the "for" list.
Cheers, everyone
# 6  
Old 07-10-2015
I think brace expansions should work in ksh (-B option) as well.
# 7  
Old 07-10-2015
@MadeInGermany. Brace expansion is not part of POSIX:
Code:
$ for DATE in 06{29,30} 07{06,07,08,09}
> do
>   echo "$DATE"
> done
06{29,30}
07{06,07,08,09}

This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

"if" statement based off "grep"

Hello, I am somewhat new to Linux/Unix. I am currently working on a shell script that is suppose to cat a file, grep the same file for a certain line, if that line is found save the file in a different location, else remove the file. This is a rough example of what I want. $Dating = False... (13 Replies)
Discussion started by: Amzerik
13 Replies

3. UNIX for Dummies Questions & Answers

What is the meaning of "-s" option in "if" statement?

Hi Guys, I'm sorry but I can't find answer for this, what is the meaning of -s option in "if" statement on unix scipting. Please see sample below: opath=/home/output for i in N1 N2 N3 N4 do echo $i if then grep $i $opath/N5_CRAI > $opath/N5_$i.crai chmod 777 $opath/N5_$i.crai ... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

What "-a" operator means in "if" statement

Hi I am trying to figure out what the following line does, I work in ksh88: ] && LIST="$big $LIST" Not sure what "-a" means in that case. Thanks a lot for any advice -A (1 Reply)
Discussion started by: aoussenko
1 Replies

6. Red Hat

"if" and "then" statement is not working in RedHat

Dear experts, I'm trying to write a script to calculate the usage of Log Archive in a directory, so if it gets to a point where the directory size is 60%, then send out an FYI.. email. So if then it reaches to 80%, move the logs from that directory. I have written the script as follow but... (10 Replies)
Discussion started by: Afi_Linux
10 Replies

7. Shell Programming and Scripting

Question about special variables: "-" and "$_"

both ksh/bash support this 2 special variables, Is there any document for reference? 1) "-" is $OLDPWD 2) "$_" is last argument of previous command. (4 Replies)
Discussion started by: honglus
4 Replies

8. Shell Programming and Scripting

ksh "case" statement question

Hi I have the following case statement: case $larg in *_* ) a=${larg%_*}; b=${larg#*_}; ;; *^* ) a=${larg%^*}; b=${larg#*^}; ;; esac I cannot figure out what *_* and *^* stand for... Also what a=${larg%_*}; b=${larg#*_}; and a=${larg%^*}; b=${larg#*^}; ... (1 Reply)
Discussion started by: aoussenko
1 Replies

9. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

10. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies
Login or Register to Ask a Question