LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-10-2010, 12:06 PM   #1
Marcopredador
LQ Newbie
 
Registered: Feb 2010
Posts: 3

Rep: Reputation: 0
How to filter script files in bash programming


hey everyone.
I'm building my first BASH programs and I have a hard time.
I can't do a search in a folder and filter only script files without extension, the problem is to differentiate the script files from the others.
I tried with ls I tried with find and i don't find a way to make it work
Can somebody help me?
 
Old 02-10-2010, 12:14 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
It's hard to help you without understanding exactly what you're trying to do and seeing the code.

From my basic understanding you are wanting to be able to perform a directory list and remove entries that are shell scripts (to be fair your scripts shouldn't be in your data directories-- they should be in /usr/local/appropriate-directory-for-function-or-item), but you can filter out things with a .sh on the end using an if statement or a find or even sed depending on the particulars of how you're calling things and specifically what you're doing. For example using -prune with find, or excluding files with the execute bit set, etc.
 
Old 02-10-2010, 12:24 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Have a look at the "file" command.
 
Old 02-10-2010, 02:16 PM   #4
Marcopredador
LQ Newbie
 
Registered: Feb 2010
Posts: 3

Original Poster
Rep: Reputation: 0
It's hard to help you without understanding exactly what you're trying to do and seeing the code.

From my basic understanding you are wanting to be able to perform a directory list and remove entries that are shell scripts (to be fair your scripts shouldn't be in your data directories-- they should be in /usr/local/appropriate-directory-for-function-or-item), but you can filter out things with a .sh on the end using an if statement or a find or even sed depending on the particulars of how you're calling things and specifically what you're doing. For example using -prune with find, or excluding files with the execute bit set, etc.

thanks for answer my call so quickly.

rweaver I'll try to explane more clearly.

I just want to list all files that are shell scripts.
I have a folder where I mixed many types of files
The files has no extension (.ch or .txt or .pdf)
There are files in that folder (Phiton and Bash script) that are the ones I want to list

catkin, was a good guess but the problem is that the script's return ASCII text as well as text, I still need to differentiate them, but I think you put me on the good track.

as soon as I find a solution I post it
 
Old 02-10-2010, 02:25 PM   #5
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
File will tell you what you want to know if the scripts are using the (fairly) standard:

#!/path/executable

First line, even if your version only returns ascii text that is valuable information (it's not a binary) and you can head -1 the file to pick up what scripting language its using... or alternate grep that file for specifics of the language that isn't going to be in other languages.
 
Old 02-10-2010, 02:33 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
find -type f | xargs file|awk 'BEGIN{IGNORECASE=1} $0 ~ /script/ && $1 !~ /script/ {print $0}'
seems to work for me
 
Old 02-10-2010, 03:13 PM   #7
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
That's a very good solution as long as they at least followed the convention of adding the executable to the top of the file. If not it'll be considerably messier.
 
Old 02-10-2010, 04:58 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
Can I just make a recommendation for the future:

1. even though they're not reqd, do use file extensions where a convention exists eg .sh
2. do put the shell prog as the first line of the script eg
#!/usr/bin/bash

If you haven't done either of these, the you're going to jave to use the 'file' cmd to get 'text' files and check/rename them by hand. hopefully not too many to do.
If I had a lot, I'd write another script to do some basic parsing eg look for bash keywords. Could be tricky unless you know your scripts well.

Another search option is looking for the 'x' permission; should only be on runnable files.
 
Old 02-12-2010, 12:01 PM   #9
Marcopredador
LQ Newbie
 
Registered: Feb 2010
Posts: 3

Original Poster
Rep: Reputation: 0
hey everyone.

first of all, I would like to thank everyone for their precious help

Tinkster, WOW Mastre solution, I'm still analyzing it.

ok, I'll explain mine solution first.

I created a test folder with ".sh" files and inside I created another folder with duplicates of my ".sh" files, (with and witout extentions), the problem is that in my later scripts (untested) I forgot to declare "#!/bin/bash" so the shell recognized "ascii". I'v rectifyed this detail and the "file *" (thanks catkin) workd wonder. after that i just filter with "fgrep" and magic happened.

here is the code you can use in /usr/bin/ fo test:
Code:
file /usr/bin/* | fgrep 'shell script'
now i whant the code to make the sherch in an file tree (it will bw an option in the script) anny suggestions

Last edited by Marcopredador; 02-12-2010 at 12:05 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to use sed for filter mutiples patterns from apache access logs matyu Programming 5 02-06-2008 10:28 PM
Bash script programming problem ArthurHuang Programming 5 01-23-2008 03:24 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration