Visit The New, Modern Unix Linux Community


How can I put wildcards in an if statement that uses variables?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How can I put wildcards in an if statement that uses variables?
# 1  
Question How can I put wildcards in an if statement that uses variables?

With the if statement:

if [ -f /098/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}-12AM.gz \
-a -f /789/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}-12AM.gz \
-a -f /456/access.${PREV_DAY_DAY}${PREV_DAY_MONTH}-11PM.gz \
-a -f /123/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}-12AM.gz ]


How can I make it so it accepts a wildcard after the ${CURR_DAY_MONTH} variable?

Putting

a -f /webtrends/SUN/mrw2/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}*

won't work, right? I think I need some kind of special character so it knows the wildcard is valid, but I am not sure what to put. Bascially, I want to check to see if the file is there, but the 12AM and the .gz part are not always present, so I don't want the check to fail simply because the file is not zipped.
LordJezo
# 2  
Try something like this:
Code:
let all_there=0
ls /098/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}*
let all_there=all_there+$?
ls /789/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}* 
let all_there=all_there+$?
ls /456/access.${PREV_DAY_DAY}${PREV_DAY_MONTH}* 
let all_there=all_there+$?
ls /123/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}* 
let all_there=all_there+$?

if [ all_there -eq 0 ] ; then
   # --------- do stuff here because the files are there
fi

You can use find as well - just something that will error out if it cannot find a file.
# 3  
What does the $? signify?
LordJezo
# 4  
I think you want to do stuff like:

ls /098/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}* >/dev/null 2>&1

That way you won't see the output.

Commands like ls return a code when they exit. In the case of ls, 0 means it worked. And ls will return a 2 or something if no files were found. In the shell $? is the exit code from the last command.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #778
Difficulty: Medium
Harmonic analysis is a branch of mathematics concerned with the representation of functions or signals as the superposition of basic waves.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Expand Variables and Wildcards into another variable.

Dear Forum members, I am having trouble getting the complete filename (and directory path) in a variable. Output directory mentioned in the code have three files: DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_london.out DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_paris.out... (4 Replies)
Discussion started by: chetanojha
4 Replies

2. Shell Programming and Scripting

How to put variables commands in config files?

Hi All, Seeking for your assistance on how to put in variables all the commands in /bin config files: /home/test/config_file/config.cfg cat /home/test/config_file/config.cfg ECHO=/bin/echo LS=/bin/lsMain script cat test.sh source=/home/test/config_file/config.cfg ECHO=$ECHO LS=$LS#i... (3 Replies)
Discussion started by: znesotomayor
3 Replies

3. UNIX for Dummies Questions & Answers

Using wildcards in variables in zsh

Probably a stupid question... how do I use a wildcard in a variable in zsh? If I do: var=* echo $var in bash, it will print all files/directories in the current directory. If I do it in zsh, it will only output an asterisk without the wildcard functionality. Thanks in advance! (1 Reply)
Discussion started by: RainbowLAr
1 Replies

4. Shell Programming and Scripting

Ways to put variables into zenity radiolist

Hey guys, i have got a problem...zenity is not being able to read my variables in its radiolist options. as zenity needs a FALSE in front of a radiolist options, i have decided to ... op=$(awk '{print "FALSE" " " $2} /etc/fstab) $?=$(zenity --list --text "mount" --radiolist --column... (1 Reply)
Discussion started by: dplate07
1 Replies

5. Shell Programming and Scripting

put a shell in for statement

I'm a newbie here, today I've got a problem. Here's the shell: b.sh #!/bin/bash rm -rf $1 a.sh #!/bin/bash for file in '/root/Desktop/test/*' do echo $file sh ./b.sh $file done ls /root/Desktop/test When I sh a.sh, the result is : (2 Replies)
Discussion started by: very.very.sorry
2 Replies

6. Shell Programming and Scripting

put value of multiple sql statements into unix variables

i want to use multple sql count statements and store these count values in unix variable but in one connection only i.e. in only 1 time database should be hit ,which is the main requirement. (1 Reply)
Discussion started by: sw@pnil
1 Replies

7. Shell Programming and Scripting

How to read a line and put it into 3 variables

Hi All, I'll get a file whose 2nd line contains 3 fields: filename(variable length), file size char(10), and record count int(10). How do I cut it and put it into 3 variables? eg: abcd.csv01234567891111111111 now I want: $one = abcd.csv, $two = 0123456789, $three = 1111111111. I also... (8 Replies)
Discussion started by: Mandab
8 Replies

8. Shell Programming and Scripting

wildcards with if statement?

Hello i am trying to use the wildcards with the if statement but it is displaying the error like this one if * | ** | * ] Any body can help me to for using the wild card option in the if case but i have used this code and working well with the case statement to enter the name without the... (14 Replies)
Discussion started by: murtaza
14 Replies

9. Shell Programming and Scripting

Put content of file into variables

How to put a line of strings (with some white spaces in between) from a file into variables? I have tried the following codes. However, the content is cut by space (not by line) for i in `cat ${source_file}` do echo $i done Please comment. Thanks. (2 Replies)
Discussion started by: Rock
2 Replies

10. Shell Programming and Scripting

How can I make an if [ -f ] statement with wildcards?

Normally you would have something like.. if then foo bar fi but what if you wanted to do something like if then foo bar fi How do I get Unix to accept anything that matches a pattern of FILENAME with anything after it during an in if statement? (3 Replies)
Discussion started by: LordJezo
3 Replies

Featured Tech Videos