LinuxQuestions.org
Help answer threads with 0 replies.
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-13-2013, 02:12 PM   #1
Don_Nadie
Member
 
Registered: Aug 2009
Posts: 44

Rep: Reputation: 0
Help with simple bash script for Linuxmint 14 32 bit


Linuxmint 14 Cinnamon 32 bit

I'm a Linux noob trying to learn some bash scripting by example.

I tried to implement a bash script which appears in the first comment at this URL: http://showmedo.com/videotutorials/v...omSeriesID=388 but it doesn't work. I get a blank box rather than an MD5 sum.

The idea is to select one or more files in Nemo, right-click on it or them, choose "Scripts" and then the script below which is in ~/.gnome2/nemo-scripts. I made it executable for everyone.

Here's the original script:
Code:
#/bin/sh

# The script "Calculate MD5" calculates the MD5 of a selected file

# You can replace the /usr/bin/md5sum with /usr/bin sha1sum to calculate sha1 instead

title="Calculate MD5"

IFS='

'

for i in ${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS}; do

md5sum "$i"

done | zenity --text-info --title="$title" --width=1100 --height=100
except that I changed the first line to
Code:
#!/bin/sh
from
Code:
#/bin/sh
and I changed NAUTILUS_SCRIPT_SELECTED_FILE_PATHS to NEMO_SCRIPT_SELECTED_FILE_PATHS, although I have tried it both ways and always get a blank box.

If I open a terminal and type "md5sum some_file_name" I get the desired md5sum. /usr/bin is in my $PATH variable. I have zenity installed and it pops up an empty box with the "Calculate MD5" title.

TIA for the help.

Last edited by Don_Nadie; 04-13-2013 at 02:14 PM. Reason: added fact that I made script executable
 
Old 04-13-2013, 02:47 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Making the change you did in the first line specifies that you want to use bash with the POSIX restriction enabled. Try the script without that change.
 
Old 04-14-2013, 10:38 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
I don't use gnome, but according to this, "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" is an automatically set environment variable containing a newline-separated list of the input files.

That's certainly not an especially safe way to handle file inputs. What if a filename itself contains an embedded newline?

In any case, the variable is unnecessary, since the script's input arguments also contain the list of input names. You only need to loop directly over them.

Code:
#/bin/bash

title="Calculate MD5"

for uri; do

    [[ -f $uri ]] && md5sum "$uri"

done | zenity --text-info --title="$title" --width=1100 --height=100

exit 0
Notice how I added a quick test so that it skips anything that's not a regular file, since md5sum doesn't work on directories.
 
Old 04-15-2013, 05:21 AM   #4
Don_Nadie
Member
 
Registered: Aug 2009
Posts: 44

Original Poster
Rep: Reputation: 0
[SOLVED] with /bin/bash rather than !/bin/bash

Thank you @PTrenholme. This script now works:
Code:
#/bin/sh

# The script "Calculate MD5" calculates the MD5 of a selected file

# You can replace the /usr/bin/md5sum with /usr/bin/sha1sum to calculate sha1 instead

title="Calculate MD5"

IFS='

'

for i in ${NEMO_SCRIPT_SELECTED_FILE_PATHS}; do

md5sum "$i"

done | zenity --text-info --title="$title" --width=800 --height=100
Note that the file manager in Linuxmint is Nemo, not Nautilus, so I have to use the appropriate environment variable. I modified the script to also make scripts that calculate SHA1 and SHA256 sums.

Sorry @David the H., but your script just gives me a blank box.

Last edited by Don_Nadie; 04-18-2013 at 11:48 AM. Reason: change [#/bin/bash to #bin/bash
 
Old 04-15-2013, 08:57 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
There's a typo in both the shell invocations above; it should start with '#!', then the path to the desired shell eg bash.
On my system
Code:
# this cmd tells you where shell lives
which bash
/bin/bash

# therefore start of script should be
#!/bin/bash

# not
#/bin/bash

# not
[#/bin/sh
 
Old 04-17-2013, 12:07 AM   #6
Don_Nadie
Member
 
Registered: Aug 2009
Posts: 44

Original Poster
Rep: Reputation: 0
@chrism01: There was a simple typo in my last post. I corrected it and thank you for your observation. The reason for editing is wrong but the script is now correct. /bin/sh works and /!bin/sh does not work. Haven't tried @David the H.'s script with /bin/sh instead of /bin/bash.
 
Old 04-18-2013, 11:50 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
I sure did miss it too. It comes from just copying and pasting the original and modifying it. I changed the interpreter name, but failed to catch the missing "!" from it.

And be clear: The shebang always starts with "#!" (hence the name, "sharp+bang"), followed by the path to the interpreter to use. Note that the first character makes it a shell comment too, so that it's ignored if sourced or run with an explicit interpereter call or something.

See the ongoing discussion here about the purpose of the shebang.
http://www.linuxquestions.org/questi...estion-762082/


In any case my version of the script is pretty much /bin/sh compatible too, except for the advanced test brackets I added. Just change this...
Code:
[[ -f $uri ]] && md5sum "$uri"
To this...
Code:
[ -f "$uri" ] && md5sum "$uri"
...and it should be good to go in any bourne-style shell.
 
  


Reply

Tags
bash, linuxmint, md5sum, zenity


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
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 10:46 AM
Simple Bash Script Filipe Linux - Newbie 2 07-17-2007 10:07 AM
need help regarding my very simple bash script hottdogg Programming 3 05-02-2007 03:09 AM
Simple bash script lp449 Linux - Networking 3 08-02-2005 05:25 PM
Simple Bash Script dmedici Programming 9 12-31-2004 04:48 AM


All times are GMT -5. The time now is 01:54 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration