Slackware This Forum is for the discussion of Slackware Linux.
|
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
01-26-2014, 10:31 PM
|
#1
|
Senior Member
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223
Rep:
|
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.
|
|
|
01-26-2014, 10:49 PM
|
#2
|
LQ Addict
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,220
Rep:
|
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.
|
01-26-2014, 11:20 PM
|
#3
|
Senior Member
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223
Original Poster
Rep:
|
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.
|
|
|
01-26-2014, 11:41 PM
|
#4
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,303
|
Quote:
Originally Posted by moisespedro
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.
|
|
|
01-26-2014, 11:47 PM
|
#5
|
Senior Member
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223
Original Poster
Rep:
|
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.
|
|
|
01-27-2014, 03:56 AM
|
#6
|
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:
|
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:
after echo statement.
|
|
|
01-27-2014, 04:06 AM
|
#7
|
Member
Registered: Jan 2011
Posts: 253
Rep:
|
|
|
|
01-27-2014, 07:55 AM
|
#8
|
Member
Registered: Dec 2008
Location: Cape Town, South Africa
Distribution: Slackware 15.0
Posts: 641
|
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.
|
|
|
01-27-2014, 11:28 AM
|
#9
|
Senior Member
Registered: Apr 2009
Location: McKinney, Texas
Distribution: Slackware64 15.0
Posts: 3,860
|
Quote:
Originally Posted by Mark Pettit
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.
|
|
|
01-27-2014, 12:59 PM
|
#10
|
Senior Member
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223
Original Poster
Rep:
|
Quote:
Originally Posted by Mark Pettit
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.
|
|
|
01-27-2014, 01:13 PM
|
#11
|
Member
Registered: Dec 2008
Location: Cape Town, South Africa
Distribution: Slackware 15.0
Posts: 641
|
@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.
|
|
|
01-27-2014, 01:25 PM
|
#12
|
Senior Member
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223
Original Poster
Rep:
|
Hmm, but first I will try to finish my first script ( here). Then, I will try python.
|
|
|
01-27-2014, 01:37 PM
|
#13
|
Member
Registered: Aug 2012
Distribution: Slackware64 15.0 (started with 13.37). Testing -current in a spare partition.
Posts: 958
|
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.
|
|
|
01-27-2014, 01:40 PM
|
#14
|
Senior Member
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223
Original Poster
Rep:
|
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.
|
|
|
01-27-2014, 02:12 PM
|
#15
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,303
|
Quote:
Originally Posted by Mark Pettit
@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.
|
|
|
All times are GMT -5. The time now is 09:43 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|