LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   New to shell scripting, any advice appreciated. (https://www.linuxquestions.org/questions/linux-newbie-8/new-to-shell-scripting-any-advice-appreciated-4175573029/)

theloaf 02-23-2016 11:36 AM

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

grail 02-23-2016 12:09 PM

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 :(

pan64 02-23-2016 12:13 PM

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

theloaf 02-23-2016 12:49 PM

thanks a lot!

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

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!


All times are GMT -5. The time now is 01:35 AM.