Help using wildcard (*) within script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help using wildcard (*) within script
# 1  
Old 08-31-2017
Help using wildcard (*) within script

Hi. I'm using !/bin/ksh. I used to be decent at shell programming years ago, but now I am extremely rusty. HELP

My team has a script that takes user input from screen. The user enters grep A0 /dir/dir1/dir2/*filename*. The code works if they enter a filename or directory. It doesn't work if user enters *filename* (or filename*). User gets 'no matches found'.
Code:
 
 func_grep()
{
getstr "Search command: " cmdstr
search_cmd=`echo $cmdstr | awk '{ print $1 }'`
 expandstr "$cmdstr" /* expandstr assigns v1_file. This part is working
cmd_opt=`echo $cmdstr | awk 'NF > 3 { print $2 }'`
search_key=`echo $cmdstr | awk '{ print $(NF-1)}'`
chk_str $v1_file /*checks for '../'
if [ $? -eq "0" ]
then
      if [ -d $v1_file ]  # if input is directory then append "*" to
      then                # search for entire directory
             v1_file="$v1_file/*"
      fi
      $search_cmd $cmd_opt $search_key $v1_file > /ercs/ercs000/reports/grep_$LOGNAME
  
 case $? in
             0) echo "\nThe grep output file: /ercs/ercs000/reports/grep_$LOG
             NAME has been created." ;;
             1) echo "\n\tNo matches were found." ;;
             *) echo "\n\tSyntax  errors  or  inaccessible  files."
                echo "\t$search_cmd $search_key $v1_file"
                return 1 ;
 esac
}

---------- Post updated at 02:55 PM ---------- Previous update was at 11:41 AM ----------

Ok no replies. In the meantime, I've done some trouble shooting at the command line and figured out that the assignment below in the expandstr function is not working when greping the 'path/*file*' because the $NF contains only the last file name in *file* (instead of all the names) which is because they are delimited by a space.

Code:
v1_file=`echo $1 | awk '{ print $NF }`

So I need to capture in a variable everything after
Code:
$search_key (/path/file1 /path/file2 /path/file3 etc)

. Any suggestions on how to accomplish that. Hope this is clear.

Last edited by Scrutinizer; 09-01-2017 at 02:25 AM.. Reason: code tags...; and again
# 2  
Old 09-01-2017
I wonder how your script can handle * wildcard at all because variables are not in quotes.
Variables in command arguments must be in quotes, otherwise the shell tries to expand them.
And where are the functions chk_str and expandstr?
# 3  
Old 09-01-2017
Thank you so much,
Yes, that is the problem. It is not working and apparently never has (don't ask - lol). When user enters a directory path or directory file name it works fine. Just to reiterate, the user enters grep A0 /dir1/dir2/*partialfilename*. partialfilename* doesn't work either. Everything is called from func_grep()

Code:
chk_str()
    23  {
    24  if [ -f "$1" -o -d "$1" ]
    25  then
    26      return 0
    27  else
    28      echo "\n\tAccess to this file/directory is not allowed."
    29      return 1
    30  fi
    31  }
  
 expandstr()
    47  {
    48  v1_file=`echo $1 | awk '{ print $NF }`
    49  cmd_opt=`echo $1 | awk 'NF > 1 { print $1 }'`
    50  v1_file=`trim_str $v1_file`
    51  if [ "$v1_file" = "/etc/passwd" -o "$v1_file" = "/etc/ftp.allow" ]
    52  then
    53      return
    54  elif [ "$v1_file" = "${v1_file#/ercs}" ]
    55  then
    56      v1_file=$TopDir/$v1_file
    57  fi
    58  }
  
 trim_str()
    15  {
    16  echo $1 | sed 's/\.\.\///g'
    17  }



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-01-2017 at 11:15 AM.. Reason: Added CODE tags.
# 4  
Old 09-01-2017
And get_str? What is get_str? And where does $v1_file come from?

Honestly, it'd be better to replace this stuff than fix it. It's all a horrible mess. Most of that top code could have been just
Code:
read cmdstr cmd_opt search_key

instead of 5 lines of echo | awk.

They're not using if-statements properly either. They could have just done

Code:
if chk_str "$arg"
then
...
else
...
fi

Also, that "/* ... */" style comment is not treated as a comment by the shell! It should be command stuff # comment stuff instead!

Last edited by Corona688; 09-01-2017 at 01:42 PM..
# 5  
Old 09-01-2017
Also, never use \n \t etc in echo. You can use them in printf, which has unique restrictions of its own. For your purposes, printf "%s\n" "string" is equivalent to echo "string". Put extra newlines in the first part, not the second part, if you need them.

Note that v1_file/* won't expand recursively. It'll fill in the lowermost directory tree and that's it.

My first try:

Code:
func_grep() {
        read cmdstr cmd_opt search_key # Read arguments from keyboard, separated by space

        check_str "$cmdstr" || return 1 # Return immediately on error

        expandstr "$cmdstr"

        [ -d "$v1_file" ] && v1_file="$v1_file/*" # Expand dir

        # $v1_file can't be quoted for * to work, everything else should be quoted.
        "$search_cmd" "$cmd_opt" "$search_key" $v1_file > "/ercs/ercs000/reports/grep_$LOGNAME"

        case $? in
                0) printf "\n%s\n" "The grep output file: /ercs/ercs000/reports/grep_$LOGNAME has been created." ;;
                1) printf "\n\t%s\n" "No matches were found." ;;
                *) printf "\n\t%s\n" "Syntax  errors  or  inaccessible  files."
                        printf "\t%s\n" "$search_cmd $search_key $v1_file"
                        return 1 ;
        esac
}


Last edited by Corona688; 09-01-2017 at 01:56 PM..
# 6  
Old 09-01-2017
I'm going to be off a while. I'll have to study your responses. I will get back with you guys. Thanks for the help. v1_file came from the expandstr function. Getstr gets the grep command the user typed in. It's all there, just out of order.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Help with wildcard

CD_numb is AM017 this code: set the_Firstcom_CD to (do shell script "ls -d '/volumes/audioNAS/Firstcom/Access Music/' ") & CD_numb gives me this: "/volumes/audioNAS/Firstcom/Access Music/AM017" the item I am looking for is AM017Q. I can get the "*" syntax right so it never finder... (7 Replies)
Discussion started by: sbrady
7 Replies

2. Shell Programming and Scripting

Wildcard in bash script

Hi there, I am pretty new to linux scripting so .. I am writing a script to loop through all my directories of sequence files in order to do stuff with them (trimming, normalizing, stuff that one would do with sequence files). Here I need to pick out files that match each other. The files... (10 Replies)
Discussion started by: jahndavik
10 Replies

3. Shell Programming and Scripting

Wildcard in ls

Hi Experts, I want to use ls in the below form: ls -l *.{txt,TXT} (working fine) but when i am declaring a variable, VAR="*.{txt,TXT}" ls -l $VAR is not working. Please help. Thanks. (4 Replies)
Discussion started by: sugarcane
4 Replies

4. Shell Programming and Scripting

wildcard help!!

i have got heaps of files (.pdf, .txt and .doc) files in one folder, i am making a program in PERL that helps me find the files i want easier using shell wildcard, something like this!! print "Enter a pattern: (must be in )"; $input = <STDIN>; if (The input is in and valid wildcard... (3 Replies)
Discussion started by: bshell_1214
3 Replies

5. Shell Programming and Scripting

How to use wildcard * in if?

Hi, Can anyone help me how to use * in if statement. File contains below line1:a|b|c|Apple-RED| line2:c|d|e|Apple-Green| line3:f|g|h|Orange| I need to find line by line 4th field contains 'Apple' or not. Please help me at the earliest. (6 Replies)
Discussion started by: jam_prasanna
6 Replies

6. Shell Programming and Scripting

Problem when concatinating wildcard onto file location in bash script

I am having difficulty with the following script: #! /bin/bash filelist=~/data/${1}* ~/./convertFile $filelist ~/temp/outputEssentially, there are a large number of files in the directory ~/data, each with a four-letter code at the beginning (eg. aaaa001 aaaa002 bbbb001 bbbb002 etc). The... (11 Replies)
Discussion started by: Lears_Fool
11 Replies

7. Shell Programming and Scripting

wildcard in bash shell script

I tried to use the wildcard '*' in my bash script, but I can not get it work. Here is a simple example (list file names in current directory): ls ./* does not work in my bash script. But it works if I use ls ./ So is there any special syntax to use '*' wildcard in bash script (I tested the... (11 Replies)
Discussion started by: aerosols
11 Replies

8. Shell Programming and Scripting

globbing, $# is too high after wildcard expansion in bash script

How can I pass in an argument such as "*.k" to a bash script without having to double-quote *.k and not having *.k `glob` to match all files in the pattern? I tried using noglob in my script but this didn't work the way I thought it would.. expansion is still occuring, $# is higher than I... (3 Replies)
Discussion started by: zoo591
3 Replies

9. UNIX for Dummies Questions & Answers

wildcard

what will the cmd below do? ls *.3 1 members mentions that to seek all permutations and combinations of the mp3 extension ill have to use curly braces, {} and not, . what then will do? (13 Replies)
Discussion started by: abhi
13 Replies

10. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies
Login or Register to Ask a Question