LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   First Script and bin directory (https://www.linuxquestions.org/questions/programming-9/first-script-and-bin-directory-4175439612/)

Ztcoracat 12-01-2012 01:47 PM

First Script and bin directory
 
;)Hi:

This is my first script and I know it probably has errors but I am trying to understand. I used gedit to make it. I have read this article to help http://www.linuxcommand.org/wss0010.php

Code:

#!/bin/bash
#my first script
echo "Hello World!"
chmod 755 my_script
./my_script
echo $PATH
export PATH=$PATH:documents
mkdir bin
my_script

Where the line that says export PATH=$PATH: Is this where I put the path to my script in my documents?
ztcoracat/documents/my_script?

And the line for mkdir bin; if I make a new directory is there only one bin that it will be dedicated to?

Does my script have to be named 'my_script"?
Can it be named my_firstscript1?

The instructions say to move my script into my new bin directory.
Where are new bin directories located? In the Home folder?

linosaurusroot 12-01-2012 03:01 PM

Your script could look like this
Code:

#!/bin/bash
#my first script
echo "Hello World!"
echo $PATH

and your commands run outside the script could look like this
Code:

chmod 755 my_script
./my_script
echo $PATH
export PATH=$PATH:~/bin
echo $PATH
mkdir bin
mv my_script bin/
my_script


Ztcoracat 12-01-2012 06:23 PM

Quote:

Originally Posted by linosaurusroot (Post 4840883)
Your script could look like this
Code:

#!/bin/bash
#my first script
echo "Hello World!"
echo $PATH

and your commands run outside the script could look like this
Code:

chmod 755 my_script
./my_script
echo $PATH
export PATH=$PATH:~/bin
echo $PATH
mkdir bin
mv my_script bin/
my_script


Thanks for showing me what belongs in the script it's self and what I'd run in the terminal.

How or where can I learn and read more to understand?

I'm asking because I need to learn how to write a script for 2 cron jobs to run and I still don't understand even after reading all this that a moderator posted for me.
I've read through all of this documentation and Bash scripting is still foreign to me.
http://www.tldp.org/
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://www.webring.org/hub/linux?w=1...hat-linux;rd=1
I didn't read through the Advanced Bash Scripting Guides because I'm still struggling with basic scripting-

Would going and learning Perl help me anymore in understanding Bash?

What else can you recommend?

PTrenholme 12-01-2012 06:54 PM

Go to the Programming forum and look at the bash scripting questions and answers. Also, try the pinfo bash command for a useful on-line reference. (Depending on your distribution, you may need to install the pinfo command, or just use the older info command.) Often whilst I program a bash script, I keep the pinfo bash output in a tab on my terminal emulator so I can refer to it as needed.

You could also look at the scripts in /etc/cron.*/ to see how they are written.

Consider asking the forum moderator(s) to move you thread into the Programming forum. (You can make the request by "reporting" your thread - there should be a button at the bottom of your first post.)

linosaurusroot 12-01-2012 07:02 PM

Perl is a great language but it wouldn't help you much with bash.

Programming needs to be learned by doing it - and having exercises that are actual small (to start with) things you want programmed.

Ztcoracat 12-01-2012 07:05 PM

Quote:

Originally Posted by PTrenholme (Post 4840957)
Go to the Programming forum and look at the bash scripting questions and answers. Also, try the pinfo bash command for a useful on-line reference. (Depending on your distribution, you may need to install the pinfo command, or just use the older info command.) Often whilst I program a bash script, I keep the pinfo bash output in a tab on my terminal emulator so I can refer to it as needed.

You could also look at the scripts in /etc/cron.*/ to see how they are written.

Consider asking the forum moderator(s) to move you thread into the Programming forum. (You can make the request by "reporting" your thread - there should be a button at the bottom of your first post.)

Thanks for enlightening me. I was unaware of 'pinfo bash'
My distro is Debian 6.0.5 Stable Squeeze AMD 64bit-

Reading through the Programming Forum is not helping but I just started so I will give it a chance. Thanks for the good advice.

Ztcoracat 12-01-2012 07:24 PM

Quote:

Originally Posted by linosaurusroot (Post 4840959)
Perl is a great language but it wouldn't help you much with bash.

Programming needs to be learned by doing it - and having exercises that are actual small (to start with) things you want programmed.

Agreed-
I hope to find more exercises that are small that I can grasp-

I'll check in my "The Debian Administrators Handbook" as there might be a few chapters that discuss Bash-

Ztcoracat 12-04-2012 05:51 PM

I found these differnt entries for Bash after the hashbang and am wondering why is the command interpreter different when these are used?

#!bin/bash
#!bin/sh
#usr/bin/perl
#!usr/awk

I looked in etc/cron.d to learn from the file. It's output is:
Code:

CRON.d
# /etc/cron.d/anacron: crontab entries for the anacron package

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

30 7    * * *  root        test -x /etc/init.d/anacron && /usr/sbin/invoke-rc.d anacron start >/dev/null

I have read this file several times but still do not understand how to write a shell script for rkhunter and chkrootkit that I would like an executable script to run. Otherwise I have been opening the terminal and running these applications manually.
Any suggestions?

linosaurusroot 12-04-2012 07:37 PM

Quote:

Originally Posted by Ztcoracat (Post 4842846)
I found these differnt entries for Bash after the hashbang and am wondering why is the command interpreter different when these are used?

#!bin/bash
#!bin/sh
#usr/bin/perl
#!usr/awk


These are different programs. And you're missing the first / after #!

Ztcoracat 12-04-2012 09:40 PM

Quote:

Originally Posted by linosaurusroot (Post 4842894)
These are different programs. And you're missing the first / after #!

Since they are different programs how do I know which one is appropriate for the task I wish to accomplish or run?

Got it; like this:

#!/bin/sh
#!/bin/bash

onebuck 12-05-2012 07:03 AM

Moderator Response
 
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.

Ztcoracat 12-05-2012 10:15 AM

Thanks; Onebuck!;)

Ztcoracat 12-05-2012 10:18 AM

Where in the script would I type my explanation of the purpose of the script?

For example:
This script is for cron jobs to run rkhunter and chkrookit @ 8 o'clock p.m. every Friday to make certain that there are not any root kits on my system-
Before or after the PATH?
And where should the PATH start from the /usr/bin or etc/cron?

Ztcoracat 12-07-2012 01:58 PM

Bash script or Terminal
 
Is this argument suitable for a script?

Quote:

run rkhunter_1.3.6-4 and chkrootkit_0.49-4 every Friday at 8 O'clock p.m.?

Ztcoracat 12-13-2012 01:02 PM

Quote:

Originally Posted by Ztcoracat (Post 4844772)
Is this argument suitable for a script?

Anyone?

Is there anywhere else I could go to better understand scripting?
And would learning C programming or C+ make Bash scripting make any more sense to me?


All times are GMT -5. The time now is 10:28 AM.