LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2016, 12:36 PM   #1
theloaf
LQ Newbie
 
Registered: Feb 2016
Posts: 2

Rep: Reputation: Disabled
Smile New to shell scripting, any advice appreciated.


I have just recently gotten into shell scripting and I have a small example that I've been toying with. I know it works, this post isn't about something not working. Rather, I'd like to ask to seasoned vets out there if you have any suggestions on the efficiency of my code or if I could improve my methods. Any helpful advice is much appreciated! Shell script below:

#!/bin/bash
echo ""

echo "Please compose a tweet of 140 characters or less [y/n]"

read answer

if [ $answer = y ] ;
then
read -p "Enter your tweet:" tweet
# echo -e "$tweet" >> $tweetnum.txt
else
echo thanks for nothing; exit
fi

let limit=140
rawchars=$(echo $tweet | wc -m | awk '{print $1}')
charcount=$(($rawchars-1))
negdelta=$(($charcount-$limit))
posdelta=$(($limit-$charcount))

if [[ ( $charcount -gt $limit ) && ( $answer = y ) ]] ;
then
echo "You FAIL! Your tweet has $charcount characters! That's $negdelta too many."
else
echo What a great tweet. Your tweet has $charcount characters. You can use $posdelta more.
fi
 
Old 02-23-2016, 01:09 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,254

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Firstly, welcome

Second, please use [code][/code] tags around code and data to maintain indentation and readability.

To your question:

I see you are on a MAC, is this where you are practicing or are you using a VM of a linux distribution?
I ask the above as MACs do not have all the same tools as current linux distributions and hence what works for others may not work for you.

So my below assumptions are based on using a current linux distro with GNU awk and bash version 4+:

1. echoing a blank line is superfluous if you then follow that with another echo. Simply use the -e option and add the newline character (\n) to your second echo

2. Your question doesn't make sense (perhaps English is not your first language). It would be clearer to say something like :- Would you like to compose a tweet [y/n]

3. read has the ability to also prompt the user using the -p option. This would allow the second echo and the read to all be encompassed on the same line

4. Your first if uses [] but a later one uses [[]]. For clarity it is better to use one and stick with it. I would suggest [[]] as it has more features and niceties to it

5. You make no attempt to test that the user has entered either 'y' or 'n' ... what if they enter 'q'

6. Assuming the user does strike the correct key, what if they enter a capital (Y). Here your test will still fail. Look up the use of ,, or ^^ in bash

7. Semi-colons are not required if the 'then' or 'do' are on a separate line

8. Further to above (and a personal preference) I would not put the 'exit' on the same line as your echo. this would once again rule out the need for a semi-colon

9. 'let' is superfluous and not required

10. 'ranwchars' is not required. Look up the following ${#var_name}

11. You have already previously tested for 'y' or 'n'. Either do not include it again or simply move the logic inside the first 'if'

12. Look up the use of (()) over [[]] for arithmetic in bash

Hopefully some of that helps. If you are using MAC and an older version of bash, some of the above may not work
 
2 members found this post helpful.
Old 02-23-2016, 01:13 PM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,128

Rep: Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271
please use [code]here comes your script[/code] to keep formatting

1. do not mix [ ] and [[ ]] , use only [[ ]] if possible
2. $(echo $tweet | wc -m | awk '{print $1}') probably that would be the length of tweet, use ${#tweet} instead
3. inside $(( )) do not need to use $, but that will only improve readability
4. if [[ ( $charcount -gt $limit ) && ( $answer = y ) ]] ; do not use ( ) here, I think that is not needed
 
1 members found this post helpful.
Old 02-23-2016, 01:49 PM   #4
theloaf
LQ Newbie
 
Registered: Feb 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Cool

thanks a lot!

This is exactly what I'm looking for. I am an English speaker just didn't bother to correct the question.

I AM on a Mac, but I want to eventually move these skills over to the true Linux side of things. Thanks for pointing out the importance there.

Thanks Again everyone!
 
  


Reply


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
LXer: Shell Scripting Part I: Getting started with bash scripting LXer Syndicated Linux News 0 04-29-2015 09:03 AM
Advice needed for moving away from shell scripting chicken76 Programming 24 10-24-2013 08:35 PM
Question regarding SuSE Linux and some file copying - advice appreciated. EnderX Linux - Software 1 11-05-2012 08:16 PM
Shell Scripting Geeks, advice and assist needed :-) WhisperiN Programming 6 09-25-2009 02:02 AM
teaching technology quandry - Advice appreciated! Mohtek General 2 11-01-2006 05:03 PM


All times are GMT -5. The time now is 04:32 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration