LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 04-19-2010, 05:25 AM   #16
Steve W
Member
 
Registered: Mar 2007
Distribution: Linux Mint 18.1
Posts: 520

Original Poster
Rep: Reputation: 44

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 '$@').

Steve

Last edited by Steve W; 04-19-2010 at 05:27 AM.
 
Old 04-19-2010, 07:07 AM   #17
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I don't use Nautilus or know much about it, but maybe Nautilus passes the selected files as arguments to the script?

I would try replacing the "[^.]*.*" (which matches all non-hidden (not starting with a dot) files) part of my script with "$@".

Last edited by MTK358; 04-19-2010 at 07:09 AM.
 
Old 04-19-2010, 07:16 AM   #18
Steve W
Member
 
Registered: Mar 2007
Distribution: Linux Mint 18.1
Posts: 520

Original Poster
Rep: Reputation: 44
Quote:
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!

Steve
 
Old 04-19-2010, 07:18 AM   #19
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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
 
Old 04-19-2010, 07:22 AM   #20
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
An explanation of the regex:

Code:
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.
 
Old 04-19-2010, 07:42 AM   #21
Steve W
Member
 
Registered: Mar 2007
Distribution: Linux Mint 18.1
Posts: 520

Original Poster
Rep: Reputation: 44
Quote:
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

cat $( cat nautilus_script_files | sort ) > $concat_file_name
rm -f nautilus_script_files

for arg
do
mv "$arg" ~/.Trash
done
 
Old 04-19-2010, 07:45 AM   #22
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
It appears that Nautilus introduces a few of its own variables.

Sadly I can't help you from here because I don't use Nautilus and don't intend to.
 
Old 04-19-2010, 07:59 AM   #23
Steve W
Member
 
Registered: Mar 2007
Distribution: Linux Mint 18.1
Posts: 520

Original Poster
Rep: Reputation: 44
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.

Steve
 
Old 04-19-2010, 08:02 AM   #24
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
If your problem is solved, mark it as solved (in Thread Tools).
 
1 members found this post helpful.
  


Reply



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
how to rename a file with weird characters in php michaeljoser Programming 4 06-29-2009 10:39 AM
Insert additional characters into filenames to rename them. Trap Linux - General 4 05-14-2009 06:25 AM
Rename files with the first three characters towards the end of the name pwc101 Programming 12 06-15-2008 06:25 PM
Rename a file with hexa decimal characters sandeshsk007 Linux - Software 13 12-13-2007 02:11 AM
rename file names with " ? characters fishbonz Linux - Newbie 2 05-31-2005 11:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:40 PM.

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
Open Source Consulting | Domain Registration