You are not logged in.

#1 2011-06-06 16:28:32

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Ompit: Critique my Bash script please~

Ompit, it's my screen shot utility that automatically uploads to ompldr and copies the link to your clipboards.

I am pretty new to Bash. I basically dug around Google and looking at basic structures and examples and winged it all together. I think it's pretty useful and has some potential to be expanded.

I have been thinking of adding an argument to upload a picture without taking the scrot image. Any other thoughts? Be nice. smile

Offline

#2 2011-06-06 17:01:04

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Ompit: Critique my Bash script please~

Hash bang has to be the first line; move it to the top. As it is now, the kernel knows it is a script but not which shell it belongs to. So it's run using the default shell which is not guaranteed to be bash.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#3 2011-06-06 17:06:59

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Ompit: Critique my Bash script please~

Fixed, thanks!

Offline

#4 2011-06-06 17:13:36

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: Ompit: Critique my Bash script please~

Well, i'm far from being a bash expert but here are my 2cents nevertheless tongue.

1. You don't need root privileges for what you are doing in that script. But if you would need root privileges [ "$UID" -ne 0 ] is enough. You don't have to create a variable for the root id.

2. I'm not sure what you want the $1 in scrot $1 /tmp/screen.png for. If you want to be able to pass arguments to scrot i think it's better to use "$@". With $1 just the first argument after ompit will get passed to scrot. With "$@" everything after ompit will get passed as a block, including spaces.

3. You can replace

  if [ -e /tmp/screen.png ]; then
    rm -rf /tmp/screen.png
  fi

with [ -e /tmp/screen.png ] && rm -rf /tmp/screen.png
And while you're at it, you can wrap /tmp/screen.png as a variable. It looks prettier and if someone ever wanted to change the value it'd be easier for him that way.

The idea of ompit is good though. Kinda like wgetpaste. For inspiration, I suggest you look there. But one thing i would enjoy and could probably easily added would be to also create a thumbnail of the scrot image and upload it too.

Last edited by demian (2011-06-06 17:22:37)


no place like /home
github

Offline

#5 2011-06-06 17:38:59

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Ompit: Critique my Bash script please~

Thanks, I took those tips and I added a feature. I added the ability to specify a file path on the command line (file won't be deleted after the upload). I suppose you can also upload other files besides an image-- it should work for any.

edit:

I suppose I should check if the user specified file is in fact a file that exists...

Last edited by Google (2011-06-06 17:40:03)

Offline

#6 2011-06-06 17:53:22

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: Ompit: Critique my Bash script please~

Some random hints:

Don't use [ if you're only using bash, see http://mywiki.wooledge.org/BashFAQ/031

Always quote your "$variables" unless you're sure they're just one word, see http://mywiki.wooledge.org/BashPitfalls … _.24target

Use the buildin type command instead of "which" (no need to use an external program for this).

In general, the mentioned wiki is a good starting point for bash programming, especially the "pitfalls" page.

Offline

#7 2011-06-06 17:56:05

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Ompit: Critique my Bash script please~

Thanks! That answers one of the problems I had discovered (file paths with spaces). That makes sense-- I will have to do some more reading. Thanks for the links~

edit:

I changed which for type-- I quickly did it and not sure if it's full-proof. If you see any errors please let me know.

edit 2:

Question, should I add in a portion in the block where a program isn't found-- it can check the distribution and then call the appropriate package manager to install the missing dependency? Or leave it up to the user? Which is generally common or better looked at?

Last edited by Google (2011-06-06 18:17:27)

Offline

#8 2011-06-06 19:27:50

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: Ompit: Critique my Bash script please~

KISS tongue


no place like /home
github

Offline

#9 2011-06-07 11:49:34

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Ompit: Critique my Bash script please~

Thanks-- I will do!

Offline

#10 2011-06-11 10:35:35

MrX
Member
From: Bavaria
Registered: 2010-07-10
Posts: 9

Re: Ompit: Critique my Bash script please~

Replace

screen_path="/tmp/screen.png"

with

screen_path="`mktemp`"

to avoid conflicts when multiple instances of the script are running at the same time.

Offline

#11 2011-06-11 13:44:42

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: Ompit: Critique my Bash script please~

MrX wrote:

Replace

screen_path="/tmp/screen.png"

with

screen_path="`mktemp`"

to avoid conflicts when multiple instances of the script are running at the same time.

... And better yet, instead of using `command` use $(command). At the very least it is easier to read.

Offline

#12 2011-06-13 03:30:35

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Ompit: Critique my Bash script please~

Thanks-- I will make a new commit in a day or so.

smile

Offline

#13 2011-06-13 08:13:55

jnguyen
Member
Registered: 2011-02-17
Posts: 139
Website

Re: Ompit: Critique my Bash script please~

Give some useful exit values: http://mywiki.wooledge.org/BashGuide/Te … xit_Status

Use $(command) rather than `command`. Backticks are deprecated: http://mywiki.wooledge.org/BashFAQ/082

You probably want to make sure something useful happens if "rm -rf $screen_path" failed (e.g. insufficient permissions).

Use "grep -E" instead of "egrep". They are the same, but egrep is deprecated.


TOMOYO Linux: Mandatory Access Control.
My AUR packages

Offline

#14 2011-07-06 03:55:37

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Ompit: Critique my Bash script please~

Thanks-- sorry for the slow reply. I updated the script. I still need to update the script on Git because I reinstalled Arch and need to setup Git again.

I will try to update Git after work~

Thanks and if there's any more tips please let me know!

Offline

#15 2011-07-19 04:24:29

RyanMcCoskrie
Member
From: North Canterbury, New Zealand
Registered: 2011-07-14
Posts: 5
Website

Re: Ompit: Critique my Bash script please~

I'm pretty sure that rm doesn't need the '-r' option to remove the file. I'm only speaking from a quick glance, but it struck me as odd that you would use the recursive option on a file that you know is not a directory. Feel free to ignore me if I missed something of course.

Offline

#16 2011-07-26 23:38:05

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Ompit: Critique my Bash script please~

RyanMcCoskrie wrote:

I'm pretty sure that rm doesn't need the '-r' option to remove the file. I'm only speaking from a quick glance, but it struck me as odd that you would use the recursive option on a file that you know is not a directory. Feel free to ignore me if I missed something of course.

Sorry for the slow reply. I removed the r flag but kept the f flag. I also moved the source over to bitbucket.

Offline

Board footer

Powered by FluxBB