LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Help with simple bash script for Linuxmint 14 32 bit (https://www.linuxquestions.org/questions/linux-software-2/help-with-simple-bash-script-for-linuxmint-14-32-bit-4175458020/)

Don_Nadie 04-13-2013 01:12 PM

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.

PTrenholme 04-13-2013 01:47 PM

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.

David the H. 04-14-2013 09:38 AM

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.

Don_Nadie 04-15-2013 04:21 AM

[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.

chrism01 04-15-2013 07:57 AM

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


Don_Nadie 04-16-2013 11:07 PM

@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.

David the H. 04-18-2013 10:50 AM

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.


All times are GMT -5. The time now is 06:33 AM.