Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
Well, I'm trying to do this as an "instant access" script from Nautilus. The idea is that if I had a few of these files that I wanted to chop off to their first four characters (and retain the extension), I would just select them, right click and select Scripts->File_chop.sh (or whatever), and it would do it, rather than using the command line. I thought it may be possible for the script to get its filenames from the files I had selected in Nautilus.
Looking at the ps2pdf script, that's exactly what it does. Somehow, it passes the name of the .ps file I have selected (only one file, I think) into the script. Is "for arg in $@" the same as "for each arg in $@"? Normally when you set up a for loop, it's doing something more than once. And what exactly is the purpose of $@? (I'd do a Google search for it, but it has trouble recognising the string '$@').
I would try replacing the "[^.]*.*" (which matches all non-hidden (not starting with a dot) files) part of my script with "$@".
Well the first thing I was looking at was the first line: I need something that says For Each File In Selection, or something. Once the script is locked into the loop, it needs to keep the extension and reduce the first bit to four characters. So I *think* the regular expression bit of the script works as it is. Could be wrong though...
Do you think I might be better off posting this in a Nautilus forum then? I mean, I'm not too sure where Nautilus finishes and scripting begins in this case!
I am still pretty sure that Nautilus would pass the files as arguments to the script, in which case this will work:
Code:
#!/bin/bash
for file in $@; do
newname=$(echo "$file" | sed --regexp-extended 's:(.{1,4}).*(\.[^.]*):\1\2:')
if [ "$file" != "$newname" ]; then
if [ -e "$newname" ]; then # if $filename exists
echo 'error: file "'"$newname"'" already exists!'
else
mv "$file" "$newname"
fi
fi
done
s: Search/Replace
(.{1,4}) Match 1-4 of any character
.* Match any amount of any character
(\.[^.]*) Match a dot followed by any amount of characters that aren't dots
:\1\2: Print the match in the first ()-delimited group, followed by what matched the second ()-delimited group.
I am still pretty sure that Nautilus would pass the files as arguments to the script, in which case this will work:
No, it only works on the first file in the select. It does not iterate through all those files selected.
However, I did find on a Nautilus script site a piece of code to "concatenate selected files in one file and move the old ones to Trash". I can't figure out which bit might apply to my situation. Can you?
#!/bin/bash
# Marius Andreiana
# Licence: GPL
#
concat_file_name=$( echo $1 | sed s/'..$'/""/g )
#cd $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
for arg
do
echo "$arg" >> nautilus_script_files
done
Ah, I see what's happening! The sample files I was using had spaces in them (which the files I will probably be doing this sort of thing on, will not). It can't handle filenames with spaces in. So your first script did work after all. Problem solved!
Thank you very much for your patience and assistance in this matter.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.