Bash scripting question for smart people ..... - Page 1
Forum section: electronics / general

author: Kilroywashere
author_rank: Regular Contributor
timestamp: March 23, 2019, 04:35:03 am
content: Hello world ... I have started to get into bash scripting .... did my first little program@!/bin/bash# My ScriptClearread -p " Press Enter to start "echo "Filename: $0"echo "Process ID: $$"echo "-------------------------------"echo "Please Enter your name:read nameecho "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 ?

author: extide
author_rank: Regular Contributor
timestamp: March 23, 2019, 05:02:34 am
content: So, bashisa 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 dochmod +x FILEto set itscript 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 againstshinstead ofbash. 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 goAnd 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.

author: Kilroywashere
author_rank: Regular Contributor
timestamp: March 23, 2019, 05:29:51 am
content: 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???

author: extide
author_rank: Regular Contributor
timestamp: March 23, 2019, 05:40:49 am
content: 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.

author: hamster_nz
author_rank: Super Contributor
timestamp: March 23, 2019, 07:27:34 am
content: 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.

author: legacy
author_rank: Super Contributor
timestamp: March 23, 2019, 10:21:52 am
content: Quote from: extide on March 23, 2019, 05:02:34 amyup, in our (DTB)project, we used something similar.Code:[*]bootwarm-start, cleaning stuff[*] mount-rootfsis /dev/sda3 writable? ... yes[*] kernel-modules-dep[*] mount-others[*] mount-swap128M of swap on /dev/sda2512M of swap on /dev/sda3/swap/512.bin[*] system-clean[*] memory-cleanReleasing the MemoryFreeing Up the Page CacheFreeing Up the Dentries and InodesFreeing Up the Page Cache, Dentries and InodesFlushing the File System Buffers[*] clock-simple[*] system-date-from-GPShas GPS already reached the fix status ... yesacquiring ZDA sentence ... done[*] machine-identify[*] hostname[*] machine-discovery-server[*] networking-bridgemybridge { eth0, eth1, eth2 }[*] networking-loopback[*] networking-gateway[*] networking-ipforwarding[*] networking-syn-protection[*] networking-iptables[*] networking-NAT[*] networking-proxyarp[*] networking-wifiinitializing { wlan0 } ... donewlan0: 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-inituart [ ttyS0 ttyS1 ttyS2 tty3 ttyS4gps ][*] ttygetagetty @ ttyS0waiting for cr-lfhAllouc-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 invokeifconfigandgrepplus 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:myNET-mac-get-direct eth0DE:AD:F1:6A:CA:FEResulting 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