Using :<<cut / cut to comment out block of bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using :<<cut / cut to comment out block of bash script
# 8  
Old 08-21-2018
: <<"cut" syntax fixed it!

Thanks
This User Gave Thanks to annacreek For This Post:
# 9  
Old 08-27-2018
Quote:
Originally Posted by annacreek
Unfortunately I have learn the "#" used at the bash scrip in this fashion #!... is really not "commented out line "
No, it is "really commented out". The whole story is this:

In OSes like DOS what is executable is determined by file extension: "something.EXE" is executable, "something.BLA" is not. In UNIX this is not the case. What is executable is determined by a filesystem flag, the "executable" flag:

Code:
# ls -l
total 180
-rwxrwxr-x 1 bakunin users  9224 Feb 15  2018 executable
-rw-rw-r-- 1 bakunin users  9224 Feb 15  2018 nonexecutable

Now this works fine for a all directly executable code like compiled source. But to run a script the mechanism is: load the interpreter, then load the script, feed it to the interpeter as input and only then let the interpreter run!

The problem is in the first step: UNIX has an abundance of interpreters: some dozen of shells, Python, Ruby, PERL, awk, ... . The list is endless. The question is: which interpreter to load?

For this there is the "magic" mechanism: when you try the file-command you will see something like this:

Code:
# file myfile
myfile: ASCII text

# file myscript
myscipt: Korn shell script, ASCII text executable

The file-command relies on a file /etc/magic in which rules for guessing the file type are laid out: something like "if the first three bytes resemble 'XYZ' then the file type is ...".

This worked fine in the beginning of UNIX - when there were two or three interpreters and they all had rather different syntax. Alas, we have different shells (Korn shell, bash) derived from other shells (Bourne shell) so that their syntax is 99% identical and such a rule set is bound to fail more often then not.

Therefore an invention was made: because everything after an octothorpe ("#" - and, yes, that is the correct name for that thing) is a comment in shell languages a "special comment" was introduced: if the FIRST line is a comment, further qualified by an exclamation mark AND if there is no whitespace before then everything following the exclamation mark is interpreted as the pathname of an interpreter which is loaded and then fed the script. This is how the system knows that for these files:

Code:
#! /bin/bash
echo hello world!


Code:
#! /my/interpreter
FOO-bar hello world!

it has to load /bin/bash in the first case but /my/interpreter in the second (to which the command "FOO-bar" hopefully makes sense). Notice that this only affects the OS kernel, which has to pick the correct interpreter! For the script itself this first line is just an ordinary comment.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

2. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

3. Shell Programming and Scripting

Cut out string in bash script

Hi all, I'm trying to extract string from variable in BASH. That's probably trivial for grep but I couldn't figure it out. I want to get name, there's sometimes digit after it, but it should be left out. STRING=http://name5.domain.com:8000/file.dat Could someone help me with that? Any... (10 Replies)
Discussion started by: cootue
10 Replies

4. UNIX for Dummies Questions & Answers

cut usage in bash

Hello, Could you put some light on this: > echo "ref_categorie=test" | cut -c1-15- test > echo "ref_categorie=test" | cut -c1-14- =test echo "ref_categorie=test" | cut -c15- test echo "ref_categorie=test" | cut -c15 t > It's executed on AIX if that matters. The man page is not very... (2 Replies)
Discussion started by: tsurko
2 Replies
Login or Register to Ask a Question