26_regex.sh (1314B)
1 #!/bin/sh 2 3 # Regular expression (regex) is also pattern searching through file 4 # names but also through text or any string. 5 # 6 # . matches any single character except newline. 7 # $ ls tests/ | grep 2. # if you want to see it tested 8 # 9 # 10 # ^ matches the start of a line 11 # $ grep ^"Long, long" file # same as below 12 # $ grep "^Long, long" file 13 # 14 # 15 # $ matches the end of a line 16 # $ grep end$ file 17 # 18 # [-] gives a range within the brackets, a-z A-Z 0-9 can be used 19 # $ ls 1[0-6]* # an easier way of completing the previous 20 # # exercise without globbing 21 # 22 # + matches one or more of the previous character 23 # $ ls tests/ | grep -E 'o+' # for regex expressions like this you 24 # # may have to have the -E option 25 # 26 # ? matches zero or more of the previous character 27 # $ ls tests/ | grep -E 'o+*' # not a helpful example 28 # 29 # You want to verify if someone has inputted a valid file from tests/ 30 # Your job is to match the file to a file if the user either inputs a 31 # number associated with that file, or the name between the number and 32 # file extension. Meaning inputs with .sh should return an error. 33 34 verify() { 35 # duplicate more or remove lines below if needed 36 ls tests/ | grep -E && exit 0 37 ls tests/ | grep -E && exit 0 38 } 39 40 verify $1 41 exit 1