LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 01-26-2014, 10:31 PM   #1
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Rep: Reputation: 195Reputation: 195
I am thinking about learning bash


And I already have an idea of what I would like to do and I'd like to hear your opinion. Have you ever heard about aui? It is an arch linux's script to set up some trivial tasks like changing the locale, choosing a repository, that sort of stuff. The only reason I want to do that is to save me time every time I do a fresh slackware install. I was thinking about something to set up multilib, a local slackware repo, locale, etc. Nothing really fancy, just a simple script that only does what it says on your screen (trying to stick to slackware's philosophy, it won't do nothing without telling you). Here is the aui script . Currently, I have no idea how to do this. I just started it and what I am doing is: I am reading the aui script and I am trying to adapt it to work on slackware. Anyways, would you use something like this? And, more importantly, do you have any tips? Any good documentation? Thanks and sorry for my english.
 
Old 01-26-2014, 10:49 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,220

Rep: Reputation: Disabled
To learn bash, I think you'd better off writing yourself simple functions from scratch (possibly doing the same things that this script does) and using them.

Also, I'd try to learn more generic shell scripting first.

IMHO even if you specifically want to learn BASH it's good to begin with the Shell command language specification as the manual page for BASH implicitly assumes this knowledge.

Only my of course.

Last edited by Didier Spaier; 01-26-2014 at 10:54 PM.
 
1 members found this post helpful.
Old 01-26-2014, 11:20 PM   #3
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
Well, I am already trying to write the code and I can't get user input working (as in showing options and asking for the user to choose one).
I am following 9.1 of this http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-9.html

This is how it looks like
Code:
echo "Would you like to enable multilib support?"
OPTIONS="Yes No"
	select opt in $OPTIONS; do
		if [ "$opt" = "No" ]; then
		echo "Ok, next step"
	elif [ "$opt" = "Yes" ]; then
		ls
	else
		clear
		echo "Please choose a valid option"
	fi
done
But it doesn't work, it always return "Please choose a valid option", no matter if I type yes or no

Last edited by moisespedro; 01-26-2014 at 11:22 PM.
 
Old 01-26-2014, 11:41 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,303
Blog Entries: 24

Rep: Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258
Quote:
Originally Posted by moisespedro View Post
Well, I am already trying to write the code and I can't get user input working (as in showing options and asking for the user to choose one).
I am following 9.1 of this http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-9.html

This is how it looks like
Code:
echo "Would you like to enable multilib support?"
OPTIONS="Yes No"
	select opt in $OPTIONS; do
		if [ "$opt" = "No" ]; then
		echo "Ok, next step"
	elif [ "$opt" = "Yes" ]; then
		ls
	else
		clear
		echo "Please choose a valid option"
	fi
done
But it doesn't work, it always return "Please choose a valid option", no matter if I type yes or no
You don't type "yes" or "no", it offers numbered options and you select 1 or 2. It works.
 
Old 01-26-2014, 11:47 PM   #5
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
Well, from what was there I thought I could type it. How do I make it with yes or no? I think it looks better.

EDIT: Tried with numbers, still doesn't work. Is the only thing I can't get it working by now, I think everything else will be easier.
EDIT2: Oh derp, now I got it jesus christ I am stupid

Last edited by moisespedro; 01-26-2014 at 11:59 PM.
 
Old 01-27-2014, 03:56 AM   #6
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
Quote:
echo "Would you like to enable multilib support?"
OPTIONS="Yes No"
select opt in $OPTIONS; do
if [ "$opt" = "No" ]; then
echo "Ok, next step"
elif [ "$opt" = "Yes" ]; then
ls
else
clear
echo "Please choose a valid option"
fi
done

well select-case statement in bash goes like:
Code:
case some_other_variable in
1)some-code here
;;
2)some-code here
;;
*)some-exception here
;;
esac
And i cant see any variable read out or input given ??
use:
Code:
read some_variable
after echo statement.
 
Old 01-27-2014, 04:06 AM   #7
BratPit
Member
 
Registered: Jan 2011
Posts: 253

Rep: Reputation: 100Reputation: 100
I found it very usefull

http://www.grymoire.com/Unix/Sh.html
 
Old 01-27-2014, 07:55 AM   #8
Mark Pettit
Member
 
Registered: Dec 2008
Location: Cape Town, South Africa
Distribution: Slackware 15.0
Posts: 641

Rep: Reputation: 304Reputation: 304Reputation: 304Reputation: 304
If you're going to go to the trouble of learning a scripting language, why not use one that's at least consistent and fairly easy. I refer of course to Python.

A (poor) implementation of your example would be :

#!/usr/bin/python
import sys, os

inp = raw_input("Would you like to enable multilib support? (Yes/No)").lower()
if inp == "no":
print "Ok, next step" # < indent here
elif inp == "yes":
os.system("ls") # < indent here
else:
print "Please choose a valid option" # < indent here

It's an almost direct translation. There are better ways of getting similar results.
Note that LQ removes my indentation, so you have to note that the code is indented where I marked it.

Last edited by Mark Pettit; 01-27-2014 at 07:57 AM.
 
Old 01-27-2014, 11:28 AM   #9
Richard Cranium
Senior Member
 
Registered: Apr 2009
Location: McKinney, Texas
Distribution: Slackware64 15.0
Posts: 3,860

Rep: Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229Reputation: 2229
Quote:
Originally Posted by Mark Pettit View Post
If you're going to go to the trouble of learning a scripting language, why not use one that's at least consistent and fairly easy. I refer of course to Python.

A (poor) implementation of your example would be :

Code:
#!/usr/bin/python                                                               
import sys, os

inp = raw_input("Would you like to enable multilib support? (Yes/No)").lower()
if inp == "no":
    print "Ok, next step"
elif inp == "yes":
    os.system("ls")
else:
    print "Please choose a valid option"
It's an almost direct translation. There are better ways of getting similar results.
Use the [CODE] tag to preserve whitespace.
 
Old 01-27-2014, 12:59 PM   #10
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
Quote:
Originally Posted by Mark Pettit View Post
If you're going to go to the trouble of learning a scripting language, why not use one that's at least consistent and fairly easy. I refer of course to Python.

A (poor) implementation of your example would be :

#!/usr/bin/python
import sys, os

inp = raw_input("Would you like to enable multilib support? (Yes/No)").lower()
if inp == "no":
print "Ok, next step" # < indent here
elif inp == "yes":
os.system("ls") # < indent here
else:
print "Please choose a valid option" # < indent here

It's an almost direct translation. There are better ways of getting similar results.
Note that LQ removes my indentation, so you have to note that the code is indented where I marked it.
I thought about python but I always wanted to learn bash, so I will try bash first.
 
Old 01-27-2014, 01:13 PM   #11
Mark Pettit
Member
 
Registered: Dec 2008
Location: Cape Town, South Africa
Distribution: Slackware 15.0
Posts: 641

Rep: Reputation: 304Reputation: 304Reputation: 304Reputation: 304
@richard - can you give a clear(er) demo of how to do that [code] thing please ?

@moisespedro - do note that Bash is really quite limiting and is full of warts. Python will cover everything that Bash does, and you gain the skill of usig a language which can be used for proper programming as well (network,web,database etc). And you can use a more shell-friendly version called Ipython.
 
Old 01-27-2014, 01:25 PM   #12
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
Hmm, but first I will try to finish my first script (here). Then, I will try python.
 
Old 01-27-2014, 01:37 PM   #13
Paulo2
Member
 
Registered: Aug 2012
Distribution: Slackware64 15.0 (started with 13.37). Testing -current in a spare partition.
Posts: 958

Rep: Reputation: 541Reputation: 541Reputation: 541Reputation: 541Reputation: 541Reputation: 541
I'm not familiar with 'select', so I would do like this
Code:
while true; do
  read -p "Would you like to enable multilib support (Yes/No)?"
  if [[ "$REPLY" =~ ^(N|n) ]]; then
    echo "Ok, next step"
    break
  elif [[ "$REPLY" =~ ^(Y|y) ]]; then
    ls
    break
  else
    clear
    echo "Please choose a valid option"
  fi
done
using 'tput' or 'echo -e "\033..."' you have better control
where place the cursor.
 
Old 01-27-2014, 01:40 PM   #14
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
This way seems easier but my entire script is based on select :|
I just want to make it work, my problems are on the script's commentaries.
 
Old 01-27-2014, 02:12 PM   #15
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,303
Blog Entries: 24

Rep: Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258
Quote:
Originally Posted by Mark Pettit View Post
@richard - can you give a clear(er) demo of how to do that [code] thing please ?

@moisespedro - do note that Bash is really quite limiting and is full of warts. Python will cover everything that Bash does, and you gain the skill of usig a language which can be used for proper programming as well (network,web,database etc). And you can use a more shell-friendly version called Ipython.
But the OP's question is specifically about learning how to do things in bash scripting language, not about looking for a different or even better scripting language. Learning python is not learning bash.
 
  


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
learning bash, where to get started? Cultist Linux - General 4 09-26-2011 03:23 PM
Learning how to use Bash Productively bartonski Linux - General 9 11-25-2009 05:24 PM
Need tips for learning 'bash' penguin386 Linux - Newbie 1 05-18-2009 07:58 PM
What is the BEST book on learning how to use Bash? Fabyfakid Linux - Newbie 1 08-28-2004 11:59 AM
Learning from shell's info bash ? jamaso Linux - Newbie 7 11-13-2001 10:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 09:43 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
Open Source Consulting | Domain Registration