LinuxQuestions.org
Help answer threads with 0 replies.
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 05-26-2015, 12:05 PM   #1
sigint-ninja
Member
 
Registered: Feb 2011
Location: Republic Of Ireland
Distribution: Debian,Centos,Slackware
Posts: 473

Rep: Reputation: 29
how can i get my simple script working


hi guys,

sick of typing sudo yum install package

so i wrote...or rather am trying to right a simple script to do this
with a simple command:

heres what i have:

#!/bin/bash
echo "Enter package name to install..."
read $P
sudo yum install $P

it doesnt seem to want to take the variable...it just runs
sudo yum install ...$p is ignored...i also tried "$p" but this
doesnt work either...what is the correct syntax to get a command to read a variable...is this possible...aaaaaaah it must be.
 
Old 05-26-2015, 12:14 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,870
Blog Entries: 14

Rep: Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112
When you define (a/k/a declare) a variable you don't use the "$". You only use the "$" when referencing the variable after it was declared.

Code:
#!/bin/bash
echo "Enter package name to install..."
read P
sudo yum install $P
By the way for a simple script like this you could just make it take the input on command line automatically because any arguments passed to it are passed in as numerical arguments $1, $2, $3 etc...

So just having your script with:

Code:
#!/bin/bash
sudo yum install $1
Means you can then run the script with <script> <package>. It will take whatever you input in place of <package> as $1 within the script.

Last edited by MensaWater; 05-26-2015 at 12:18 PM.
 
Old 05-26-2015, 12:25 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,764

Rep: Reputation: 745Reputation: 745Reputation: 745Reputation: 745Reputation: 745Reputation: 745Reputation: 745
or even just:

Code:
alias yinst="sudo yum install "
which allows you to specify multiple packages and bash will just add them to the command line.
Command tab-completion works with aliases as well.
 
1 members found this post helpful.
Old 05-26-2015, 12:27 PM   #4
sigint-ninja
Member
 
Registered: Feb 2011
Location: Republic Of Ireland
Distribution: Debian,Centos,Slackware
Posts: 473

Original Poster
Rep: Reputation: 29
ok such a silly mistake...my P wasnt capital...changed it and its working fine

just a quick one

i have my scripts in a directory on another partition

i ran the command:
PATH=$PATH:/mydirectory/scripts/sninja

and this worked, till i restarted the terminal

is this the right way to edit the path? and i cannot seem to find where the PATH variable is as my path is changed but i wantto edit it again...

i have looked in

/etc/bashrc
/etc/profile
/etc/profile.d
home/user/.profile
home/user/.bashrc

all the places i thought it would be

imon centos 7...can anybody please help

Last edited by sigint-ninja; 05-26-2015 at 06:07 PM.
 
Old 05-26-2015, 07:46 PM   #5
mralk3
Member
 
Registered: May 2015
Posts: 869

Rep: Reputation: 278Reputation: 278Reputation: 278
how can i get my simple script working

This would be much simpler with a bash alias in you .bashrc file:

alias install="sudo yum install"

Save your .bashrc and then type:

source ~/.bashrc

More on bash aliases:

http://tldp.org/LDP/abs/html/aliases.html
 
Old 05-26-2015, 08:07 PM   #6
sigint-ninja
Member
 
Registered: Feb 2011
Location: Republic Of Ireland
Distribution: Debian,Centos,Slackware
Posts: 473

Original Poster
Rep: Reputation: 29
hi mralk3

i knew when i started doing this that there must be an easier way...like you have shown me.
i did it purely as an exercise to know how to create a script and have it available to run
everytime i restart the computer and login.

what would really be classed as a real scriptable (if there is such a word) situation?

thanks for helping...
 
Old 05-26-2015, 08:34 PM   #7
mralk3
Member
 
Registered: May 2015
Posts: 869

Rep: Reputation: 278Reputation: 278Reputation: 278
Quote:
Originally Posted by sigint-ninja View Post
what would really be classed as a real scriptable (if there is such a word) situation?
Automating a simple or complicated task with cron, parsing a document or log file, scraping the web. Those are just a few ideas. In general I would suggest you pick up a book like Learning the bash Shell or the bash Cookbook. There's also ksh, csh and other *nix shells to be aware of. In general though, Linux comes with bash installed so I suggest you learn to shell script in sh or bash. You can also check out http://bash.cyberciti.biz/guide/Main_Page.

Once you learn how to script well in one of the *nix shells, I suggest you learn to make your scripts cross platform.

Quote:
thanks for helping...
No problem. I actually learned shell scripting in a study group that met outside my college that taught ksh and went with it from there. I have purchased a few books on the various shells, but it is possible to learn to shell script just from searching the web.
 
Old 05-27-2015, 07:30 AM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,870
Blog Entries: 14

Rep: Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112Reputation: 1112
Or just type "bash tutorial" in you favorite search engine.
 
Old 05-27-2015, 09:50 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,367

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Quote:
Originally Posted by sigint-ninja View Post
ok such a silly mistake...my P wasnt capital...changed it and its working fine

just a quick one

i have my scripts in a directory on another partition

i ran the command:
PATH=$PATH:/mydirectory/scripts/sninja

and this worked, till i restarted the terminal

is this the right way to edit the path? and i cannot seem to find where the PATH variable is as my path is changed but i wantto edit it again...

i have looked in

/etc/bashrc
/etc/profile
/etc/profile.d
home/user/.profile
home/user/.bashrc

all the places i thought it would be

imon centos 7...can anybody please help
that is simply explained on the man page of bash, just look for invocation
 
  


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
[SOLVED] working on a script santosh0782 Linux - Newbie 4 03-07-2014 03:17 AM
[SOLVED] Script not working in cron but working fine manually jasperux Linux - Newbie 2 07-04-2012 11:28 PM
[SOLVED] My script is not working right kenny53067 Linux - Newbie 5 07-24-2011 01:08 AM
Crontab is not working, the script is working arfal SUSE / openSUSE 6 02-08-2010 08:48 PM
Help get this script working please rickenbacherus Linux - General 2 03-24-2004 06:25 PM


All times are GMT -5. The time now is 07:28 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