Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've got a fairly simple problem to be solved. I have a number of directories that contains files with different extensions. I want to run a script that gives different outputs for a given extension within a chosen directory.
I have a program (which I didn't write) which runs with a number of different input files. There are around six different file types that can be used, each file type being used differently by the script, and the script can utilise any number of each file type (usually one, but sometimes more). Each collection of files that go together are stored in their own directory (including one or more files of different file type), and when I run my script I want it to work on all files in that directory.
At the moment, I have to call the script by manually typing:
As you can imagine, it gets tedious to do it this way. What I want to do is run a script where I specify the directory and it calls my other script with the parameters determined automatically, so that I only need to type:
Code:
./newscript directory1
I've tried a couple of times to work my way into bash, but I don't think that I'm going to manage it without a significant amount of time learning bash properly and motivation in the form of a more sustained need for the knowledge (my needs are simple and sporadic). My attempts to find answers myself never seem to work. For example, I've found that echo *.abc almost does the job (returning "file1.abc file2.abc file3.abc"), but I'd like to be able to do more manipulation than *.abc allows (such as prefix each file with a path).
Thanks for the link through. I'll need to sit down and give it some attention.
For your script/command "rundifferent" to know what is -files and which is -extra you must either hard code the value that you want or accept it as a command parameter.
Sample Code:
Code:
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Usage $0 <extension>"
else
echo "You specified an extension off $1"
echo
echo "files : Files matching $1 extension:"
ls -1 *.$1
echo
echo "extra : Files not matching $1 extension"
ls -1 | grep -v \.$1
fi
Sample output:
Code:
$ sh ./rundifferent.sh
Usage ./filelist.sh <extension>
$ sh ./rundifferent.sh abc
You specified an extension off abc
files : Files matching abc extension:
file1.abc
file2.abc
file3.abc
extra : Files not matching abc extension
about.xyz
file1.txt
file2.txt
file3.txt
rundifferent.sh
support.xyz
$ sh ./rundifferent.sh txt
You specified an extension off txt
files : Files matching txt extension:
file1.txt
file2.txt
file3.txt
extra : Files not matching txt extension
about.xyz
file1.abc
file2.abc
file3.abc
rundifferent.sh
support.xyz
$ ls -1
about.xyz
file1.abc
file1.txt
file2.abc
file2.txt
file3.abc
file3.txt
rundifferent.sh
support.xyz
This is just a quick script to show how I understood your question, I have not fully tested it.
I've tried a couple of times to work my way into bash, but I don't think that I'm going to manage it without a significant amount of time learning bash properly and motivation in the form of a more sustained need for the knowledge (my needs are simple and sporadic).
I am going to divulge the secrets to learning BASH:
One command at a time
man -k <keyword>
man <command>
trial and error
I am going to divulge the secrets to learning BASH:
One command at a time
man -k <keyword>
man <command>
trial and error
Very good advice; so much of using bash is automating execution of commands; to do that successfully you need to know a) what the commands are b) how to use them and c) what they output.
bash as a programming language is relatively small (but non-intuitive) so getting started with it is relatively easy (mastering its nuances may be more than many of us can do in a life time!).
If you have used C++ in a Windows environment, the situation is analogous; C++ itself is relatively simple while the Win32 interface libraries are immense and complex. Mastering the nuances of C++ may be easier than bash, though.
And you might want to run pinfo find with special attention to the "Action" section. (Install the pinfo command if you haven't already done so. In my opinion it provides a much better interface to the info files.)
My point is that you may not need to use a bash script to accomplish your goal.
Ian, interesting as it is the code you produced doesn't quite capture the problem fully due to the way in which it outputs the information (a new line per result).
From the general responses it does look as though I'm going to have to do it the hard way and do lots of reading.
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Usage $0 <extension>"
exit 0
fi
# For testing, just copy the temp file to stdout.
# For "real," use something like tmp=/tmp/sdf.sh
tmp="/dev/stdout"
echo '#!/bin/bash' >$tmp
echo -n ". $PWD/rundifferentsript -files " >$tmp
for f in $(ls -Q *.$1)
do
echo -n "$f"' ' >$tmp
done
echo -n "-extra " >$tmp
for f in $(ls -Q --ignore='*.'"$1")
do
echo -n "$f"' ' >$tmp
done
echo >$tmp
# To run the temp file, uncomment the following (the "rm" is for housekeeping).
#chmod u+x $tmp
#. $tmp
#rm -f $tmp
And here's a sample output from my bash scripts directory:
Code:
$ ./Ian.sh sh
#!/bin/bash
. /home/Peter/Scripts/bash/rundifferentsript -files "Convert to ext4.sh" "Ian.sh" "run-mozilla.sh" -extra "child" "Emerald_replace" "main" "mount_all_images_in" "new_script" "RepairDamagedCD" "Uncompress_Books
Note that, instead of the echo -n command used to output the text without a new-line at the end, you could, instead, use a "\" as the last character in file line and get something like this:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.