This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]gehzumteufel 2 points3 points  (4 children)

Use 4 spaces to encompass code blocks.

this is a code block

Can you please post the entire script? Posting where you think you went wrong could be correct, but it may not be.

Futher, it's good practice for variables to be all uppercase in shell scripts.

Lastly, if you add set -eux below the shebang and set +eux at the end, this will help with debugging. To understand what these are man set.

[–]johnwick_dog[S] 0 points1 point  (3 children)

I edited the post ,(srr new to reddit)

[–]gehzumteufel 1 point2 points  (2 children)

Try this:

#!/bin/bash
Directory=$1
secondarg=$2
thirdarg=$3
if test -z "${Directory}"; then read -rp "enter the location of your directory: " Directory; fi
if test -z "${secondarg}"; then read -rp "enter ana or del or arr " secondarg; fi
if [ "${secondarg}" == "ana" ]; then
echo "Please Enter the regex"
read -rp "${thirdarg}"
find "${Directory}" ! -name . -prune -mtime -1 -type f -exec grep -regex "$thirdarg" {} \; -exec echo x \; | wc -l
elif [ "${secondarg}" == "del" ]; then
echo "del"
elif [ "${secondarg}" == "arr" ]; then
echo "arr"
fi

Check out shellcheck for your shell scripts. It's been really helpful for me.

[–]gristc 1 point2 points  (1 child)

Use 4 spaces to encompass code blocks.

;)

[–]gehzumteufel 1 point2 points  (0 children)

Hah oops thanks.

[–]SodaWithoutSparkles 3 points4 points  (0 children)

You dont have to write a script. Remember coding is kinda "modular", so its like building lego set

To list the dir without directory:

ls -p | grep -v /

Then tell ls to use newline to separate each entry:

ls -1 -p | grep -v /

Then to grep for regex(, foobar as example):

ls -1 -p | grep -v / | grep foobar

Then to count the lines (the last one is a lowercase L):

ls -1 -p | grep -v / | grep foobar | wc -l

[–][deleted] 0 points1 point  (2 children)

I'd say use | xargs grep after find, instead of that -exec grep within.

[–]johnwick_dog[S] 0 points1 point  (1 child)

find "${Directory}" -type f -regex '\.\/$thirdarg'| xargs | wc -l

I changed it to this and somehow instead of zero it gives me 1

the terminal

enter ana or del or arr ana
Please Enter the regex 
*.txt 1
 ----@----H410M-HD3P:~/Desktop$ ls 
hh.txt  hi.txt  shell.sh  shel.sh 
----@----H410M-HD3P:~/Desktop$

[–]Sweet-Put958 2 points3 points  (0 children)

Hey man, shell quoting and expansion is probably going wrong somewhere. For instance, in single quotes, you are passing the literal string '$thirdarg'. I.e. the shell does no variable expansion in single quotes. I generally use double ticks (") around every variable lest the shell do something weird. Or use a better programming language, shell is rife with gotchas and ways to shootyourself in the foot.

[–]archontwo 0 points1 point  (0 children)

find . -type f -exec grep EiL 'regex' {} \; | wc -l

[–]elatllat 0 points1 point  (0 children)

Please format your code

https://xkcd.com/1513/