Sponsored Content
Top Forums Shell Programming and Scripting Help using wildcard (*) within script Post 303002745 by katolb75 on Thursday 31st of August 2017 02:55:07 PM
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 01:25 AM.. Reason: code tags...; and again
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
times(1)                                                           User Commands                                                          times(1)

NAME
times - shell built-in function to report time usages of the current shell SYNOPSIS
sh times ksh times DESCRIPTION
sh Print the accumulated user and system times for processes run from the shell. ksh Print the accumulated user and system times for the shell and for processes run from the shell. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
ksh(1), sh(1), time(1), attributes(5) SunOS 5.10 15 Apr 1994 times(1)
All times are GMT -4. The time now is 01:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy