Fli
09-27-2013, 01:43 PM
Hello,
here is the example script which allows linux user to search some phrasse inany folder and its subfolders (ie /home/admin) or in current directory (.) or within whole filesystem (/)
There you can learn:
- how to make script ask you input and save this input for future use as a variable
- how to run command from within bash script and print its output/use it as a variable
i created the script file "search" and made it executable (chmod +x) so i can launch it later by command "/.search" instead of "sh search.sh" and then opened it (vi editor)
touch search;chmod +x search;vi search
after opening, added this code:
echo "ABOUT: This sript allows searching in files
-------------------------------------------
1/2 Please enter search directory:
- Examples: / . ./ /home/admin/
- Your current directory: $(pwd)"
read dir
echo "2/2 Please enter search phrasse/word:"
read phrasse
find $dir | xargs grep "$phrasse" -ls;
As you may see, script writes some text by "echo" command, then it reads the users keyboard input by command "read" and saves it to variable i named "dir", then it asks another input of search phrasse (by command "echo") and saves it then into variable i named "phrasse". In final step, the variables got from keyboard input are added into the search command itself (find * | xargs grep * -ls).
Also in one of the "echo" i provide result of command "pwd" which shows current directory you are in (in command line).
- Your current directory: $(pwd)"
To do it, i just created a new variable which starts with dollar sign ($), but to tell the bash to load this variable content from command, i added "( )" around it.
here is the example script which allows linux user to search some phrasse inany folder and its subfolders (ie /home/admin) or in current directory (.) or within whole filesystem (/)
There you can learn:
- how to make script ask you input and save this input for future use as a variable
- how to run command from within bash script and print its output/use it as a variable
i created the script file "search" and made it executable (chmod +x) so i can launch it later by command "/.search" instead of "sh search.sh" and then opened it (vi editor)
touch search;chmod +x search;vi search
after opening, added this code:
echo "ABOUT: This sript allows searching in files
-------------------------------------------
1/2 Please enter search directory:
- Examples: / . ./ /home/admin/
- Your current directory: $(pwd)"
read dir
echo "2/2 Please enter search phrasse/word:"
read phrasse
find $dir | xargs grep "$phrasse" -ls;
As you may see, script writes some text by "echo" command, then it reads the users keyboard input by command "read" and saves it to variable i named "dir", then it asks another input of search phrasse (by command "echo") and saves it then into variable i named "phrasse". In final step, the variables got from keyboard input are added into the search command itself (find * | xargs grep * -ls).
Also in one of the "echo" i provide result of command "pwd" which shows current directory you are in (in command line).
- Your current directory: $(pwd)"
To do it, i just created a new variable which starts with dollar sign ($), but to tell the bash to load this variable content from command, i added "( )" around it.