Author Topic: Bash scripting question for smart people .....  (Read 1685 times)

0 Members and 1 Guest are viewing this topic.

Offline KilroywashereTopic starter

  • Regular Contributor
  • *
  • Posts: 112
  • Country: ca
  • He has the stink of oil and electric circuitry...
Bash scripting question for smart people .....
« on: March 23, 2019, 04:35:03 am »
Hello world ... I have started to get into bash scripting .... did my first little program

@!/bin/bash
#  My Script
Clear
read -p " Press Enter to start "
echo "Filename: $0"
echo "Process ID: $$"
echo "-------------------------------"
echo "Please Enter your name:
read name
echo "Nice to meet you, $name "

( whats the difference of saving this file as a .sh or just using ./ to execute)

Ok so i wanted to know what you think of bash itself .... I was interested in bash  after trying to learn how to do some things in linux .....

How near to the 0's and 1s  is bash? (Like it reminded me of basic ....)
Do you have any good tutorials you can share with me?


I know that bash runs on a shell  but what does the shell run on??? Im kinna going down a rabbit hole here ..... can anyone give me some understanding on how this all works ?

« Last Edit: March 23, 2019, 04:38:48 am by Kilroywashere »
He is part of the dead...he has no place here. He has the stink of oil and electric circuitry about him. He is obsolete...
 

Offline extide

  • Regular Contributor
  • *
  • Posts: 95
  • Country: us
    • Rovitracker - Rental management AND Real-Time data!
Re: Bash scripting question for smart people .....
« Reply #1 on: March 23, 2019, 05:02:34 am »
So, bash is a shell. That basically means it is an interface that allows you to use the computer. It is an actual binary executable that runs directly on the cpu. (Written in C and compiled to native machine code)


Regarding your question about saving the file as a .sh -- linux doesn't care about the file extension. You could happily leave the extension off entirely and it would work just the same. You need two things to make a script work:
  • Execute bit (+1 in hex) on the file -- you can just do chmod +x FILE to set it
  • script interpreter at the first line (#!/path/to/executable)
For #2 when you execute a file the shell will take a look and if it looks like a text file AND the first line has the "shebang" which is the characters #! and then a path to an executable, it will consider that file a script and pass it to the interpreter in that path. For a bash script you want to have the path to bash here. This is pretty powerful, and how you can make perl scripts, python, php, etc all executable directly from the command line. You could even write your own program that then interprets scripts and put it here. I think you may even be able to chain other scripts here, but I'm not sure.


Bash has a pretty powerful scripting language and you can do a lot of stuff with it. In fact scripts such as these are heavily used in most linux distributions for stuff like starting/stopping services and such. Although those scripts tend to be written against sh instead of bash. Bash is basically a clone of sh but is more advanced and supports more stuff. If you write scripts against sh you can be more portable because bash may not be available on every computer but sh is likely to be. HOWEVER I would not worry about that bit right now and in all reality bash is going to exist pretty much everywhere anyways.


There are lots of guides and examples for bash scripting online and I will leave it up to you to find some.


So, in terms of how near to the 0's and 1's is bash, well, not very, but that doesn't matter. I would say it goes like this:
  • Directly writing assembly is pretty much as low as you can go
  • And then the next level up is writing code that compiles directly to native machine instructions (like c or c++)
  • Then your next level up would be code that compiles into an intermediate language that is then ran in a virtual machine (like java, c#, or scala)
  • Then you have interpreted scripts which are not even compiled at all but are interpreted right when they are ran (like bash scripts, php, or javascript).
If you are going for efficiency then being higher up in that list might be important, although honestly there is some pretty fast code lower down in that list nowadays. If you are just learning stuff, then don't worry, this isn't really important.


Hopefully that all helps and I'm sure others will chime in with more.
 
The following users thanked this post: Kilroywashere

Offline KilroywashereTopic starter

  • Regular Contributor
  • *
  • Posts: 112
  • Country: ca
  • He has the stink of oil and electric circuitry...
Re: Bash scripting question for smart people .....
« Reply #2 on: March 23, 2019, 05:29:51 am »
That helped a bunch , Thank you so much for your info!

So just one last question... lets say you want to make a widget with a simple'ish display menu ,  I could get a micro processor  to have a shell on it  and use bash to make my menu.... How would you program a shell onto a chip?


Basically i would like to know how to make this menu....  would c++ be the best ? i have only really programmed Atmel chips sooo yeah just guess im just confused ????

Like a good example is a cell Phone .....  how many levels does that chip have?

Like the good old C64.... how did they get basic  on that computer???
« Last Edit: March 23, 2019, 05:34:08 am by Kilroywashere »
He is part of the dead...he has no place here. He has the stink of oil and electric circuitry about him. He is obsolete...
 

Offline extide

  • Regular Contributor
  • *
  • Posts: 95
  • Country: us
    • Rovitracker - Rental management AND Real-Time data!
Re: Bash scripting question for smart people .....
« Reply #3 on: March 23, 2019, 05:40:49 am »
Well, getting bash to run on a microcontroller would probably be pretty hard, although I'm sure it's possible. It would probably be easiest to use c/c++ for this. Check out the arduino stuff -- they have a whole bunch of easy to do stuff and a code editor and everything. The arduino sketches are pretty simple but in reality they are actually real c code so it's good stuff to learn. You can pretty easily make a little text based menu that you can interact with over a serial port.


Modern cell phones run full on modern operating systems. Android phones run a very customised version of linux -- although with a very different 'userland' running on top of it. It's the linux kernel underneath but pretty much all of the rest of the software running on top of that is very different from a traditional linux distro. Most of the applications are java which runs inside of a custom java vm that google has made. It actually is a bit different than regular java though in that it compiles the code much closer to native machine code instead of translating from the intermediate representation on the fly.


In any case you can actually run bash and bash scripts on an android phone pretty easily.
 
The following users thanked this post: Kilroywashere

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2862
  • Country: nz
Re: Bash scripting question for smart people .....
« Reply #4 on: March 23, 2019, 07:27:34 am »
Are you running it on Windows? That first line looks a little funky. It should begin with '#!' not '@!'.

When in Bash you can run scripts two ways.

1. Run them like any other program. This makes them run in a new process, so any commands that change that alter the process"s environment (e.g. Change into a directory or set an environment variable) will not occur in the user's original process.

2. 'dot' or 'source' a script. This will act like the user typed this command at the bash prompt, so can do things like set environment variables or change the current directory for the bash process.

I have done quite a bit of bash scripting back in the day. You really want to consider another tool if you want to do anything fancy on the screen, or parse user input properl, or have security in mind. For just a 'what action do you want to do...'  menu they can work fine.

Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Bash scripting question for smart people .....
« Reply #5 on: March 23, 2019, 10:21:52 am »
used in most linux distributions for stuff like starting/stopping services

yup, in our (DTB) project, we used something similar.

Code: [Select]
[*]boot
warm-start, cleaning stuff
[*] mount-rootfs
is /dev/sda3 writable? ... yes
[*] kernel-modules-dep
[*] mount-others
[*] mount-swap
128M of swap on /dev/sda2
512M of swap on /dev/sda3/swap/512.bin
[*] system-clean
[*] memory-clean
Releasing the Memory
  Freeing Up the Page Cache
  Freeing Up the Dentries and Inodes
  Freeing Up the Page Cache, Dentries and Inodes
  Flushing the File System Buffers
[*] clock-simple
[*] system-date-from-GPS
has GPS already reached the fix status ... yes
acquiring ZDA sentence ... done
[*] machine-identify
[*] hostname
[*] machine-discovery-server
[*] networking-bridge
mybridge { eth0, eth1, eth2 }
[*] networking-loopback
[*] networking-gateway
[*] networking-ipforwarding
[*] networking-syn-protection
[*] networking-iptables
[*] networking-NAT
[*] networking-proxyarp
[*] networking-wifi
initializing { wlan0 } ... done
wlan0: ad-hoc, essid "rb532", 2.4GHz, no-powersave ... done
[*] env-shared-libraries
[*] http-tini-server
[*] sftp-server
[*] ssh-server
[*] tftp-server
[*] iperf-server
[*] issue
[*] uart-init
uart [ ttyS0 ttyS1 ttyS2 tty3 ttyS4gps ]
[*] ttyget
agetty @ ttyS0
waiting for cr-lf
hAllo

uc-rb532 login:

There are bash-scripts in /etc/system/init/ invoked during the bootstrap, but we have written a lot of helpers in C so scripts can speed up.

Considering that it's a small router with a slow CPU, the whole bootstrapping process takes only 4 seconds, this thanks to fast scripts and their helpers.


e.g.
a couple of scripts need to get the MAC-address of ethernet-devices, and without a proper helper you would have to invoke ifconfig and grep plus other programs for just grabbing the information, while one of our C-helper is directly able to get the information. Just one shot and boom, you have it!

Code: [Select]
myNET-mac-get-direct eth0
DE:AD:F1:6A:CA:FE

Resulting 1 process to be invoked in the bash script, instead of 2-3-4 processes piped together: 3x faster!


Anyway, bash is a lot of great stuff for this  :D
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->