Explanation for Scripts Inner Workings Needed


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Explanation for Scripts Inner Workings Needed
# 1  
Old 03-31-2019
Explanation for Scripts Inner Workings Needed

Code:
#!/bin/bash 
n=$l; typeset -a v
x=$(< input.dat)
check(){
if[$l -le 5 -a $l -gt 2]; then
sed 's/Test/Proc/g' file.sh >fl.sh
else
 exit 13
fi
}
check $n
while [$x -ge $n]; do
 x=`expr $x -l`
v[$x]=$x
done
less fi.sh l>/dev/null&& echo yes || exit 1
echo v= ${v[*]}
exit 0

I have file.sh and input.dat in the current filepath.
Can somebody explain what
Code:
x=$(< input.dat)

and
Code:
check $n

and
Code:
less fi.sh l>/dev/null&& echo yes || exit 1

do?
and what the output of
Code:
file.sh 3

would be and why?

Last edited by bananasprite; 03-31-2019 at 08:46 AM..
# 2  
Old 03-31-2019
Welcome to the forum.


I'm afraid I have to say that your script brims over with syntax and semantic, and maybe even design errors; not sure if and how to cope with all of them. But, your questions first:





Quote:
Originally Posted by bananasprite
... I have file.sh and input.dat in the current filepath.
Is file.sh an input file or the script itself? Is input.dat a file with just one number in it, or some (lengthy) text?

Quote:
Code:
x=$(< input.dat)

That's a "Command substitution" which "allows the output of a command to replace the command name ..." (man bash) and to be assignedf to the x variable.

Quote:
Code:
check $n

That calls the afore defined function check with one argument, $n.


Quote:
Code:
less fi.sh l>/dev/null&& echo yes || exit 1

The sense of that construct escapes me. It prints two files - fi.sh and l - to nirvana, and checks the exit code. Is that just a test for file existence?

Quote:
Code:
file.sh 3

Not clear (see above), what file.sh would be. If it is your script, it'd crash with several syntax errors.







Code:
#!/bin/bash 
n=$l; typeset -a v                   # as l is undefined, n will assume the empty string value
x=$(< input.dat)
check(){
if[$l -le 5 -a $l -gt 2]; then       # spaces missing around the square bracket; l undefined, again
sed 's/Test/Proc/g' file.sh >fl.sh   # modifies file.sh and prints this to fl.sh, never to be used any more
else
 exit 13
fi
}
check $n
while [$x -ge $n]; do                # spaces missing around the square bracket;  
 x=`expr $x -l`                      # spaces missing around the minus sign; 
v[$x]=$x                             # might work if $x is a single number
done
less fi.sh l>/dev/null&& echo yes || exit 1
echo v= ${v[*]}
exit 0


Last edited by RudiC; 03-31-2019 at 11:00 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-31-2019
Quote:
Originally Posted by RudiC
Code:
x=$(< input.dat)

That's a "Command substitution" which "allows the output of a command to replace the command name ..." (man bash) and to be assignedf to the x variable.
To expand on what my colleague RudiC already said:

The command executed in this command substitution (and which' output will replace its name) is

Code:
< input.dat

By redirecting the input of the (sub-) shell it will simply make the shell read the file. Because this is all the shell instance will do it will simply replicate it to its stdandard output and finally, when the subshell is left, this output will be picked up by the assignment operation. So basically what it does is assign the variable x with the contents of the file input.dat.

I hope this helps.

Moderator's Comments:
Mod Comment On a side note: PLEASE create meaningful, concise titles for your threads. "Help with Script" would be true for absolutely every thread in a forum dealing with shell scripting, won't you think?. I changed your title to something more meaningful.

bakunin
# 4  
Old 03-31-2019
IMHO this article hits the nail:

Quote:
line=$(cat input) is the POSIX way of reading the entire file. It requires a fork.

line=$(< input) is a marginally more efficient Bashism for reading the entire file. It also forks, but doesn't have to execve.
A plain command (that is not subject to a substitution)
Code:
< input

might not work, even not in bash.

Last edited by MadeInGermany; 03-31-2019 at 03:46 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Value Too Great for Base Error, Explanation and Workout needed

Hey Friends, its me again! :o I was asked to create a script that would go into our backup directories and delete/purge anything in the directory after a certain amount of days, normally I would be able to write something up that goes to the directory finds it and deletes it. cd... (12 Replies)
Discussion started by: gkelly1117
12 Replies

2. Shell Programming and Scripting

Little explanation needed on array

I had gone through..google search.....and unix user post.......where I found so many ways of accessing files..... suppose if I am having 4 files, each file is having 3 columns, and I want to use each field of each column, then how can I use it.. how can I create array for each file's each column,... (8 Replies)
Discussion started by: Dona Clara
8 Replies

3. Shell Programming and Scripting

Perl scripts: requesting for logic explanation

Hi, balajesuri and durden_tyler, I have found your perl script for the thread https://www.unix.com/shell-programming-scripting/176370-perl-script-help-me-extracting-string.html, but find it difficult to understand the syntax. Could you or any forum members kindly shed some light on the logic... (3 Replies)
Discussion started by: royalibrahim
3 Replies

4. UNIX for Advanced & Expert Users

Grep - Explanation needed.

grep -E '^(++){5}5000' <file_name> this command searches value 5000 in only 6th column from provided file where pipe ( | )is delimiter which separate columns... can some one plz explain me what '^(++){5}5000' actually does..? :confused: (1 Reply)
Discussion started by: Killer420
1 Replies

5. Programming

Python 3.1 TypeError explanation needed

Could someone explain why Python 3.1 errors out below? Do I need an additional module that's not required in 3.2 perhaps? I need to use 3.1 as it's the version available on a server I am using. Python 3.2.1rc1 (default, May 18 2011, 11:01:17) on linux2 Type "help", "copyright", "credits"... (0 Replies)
Discussion started by: jelloir
0 Replies

6. Shell Programming and Scripting

Explanation Needed

Hi all, I'm very new to UNIX. I have got a coding, where i dont understand the below part. Could someone please explain it in detail? awk 'NR > 1; NR == 1 { S = $0 } END { print S }' $textfile.bak > $textfile could someone explain what awk 'NR > 1; NR == 1 { S = $0 } END { print S }' ... (1 Reply)
Discussion started by: raghulshekar
1 Replies

7. Solaris

showrev output explanation needed

hi this is the output of showrev command from my sun blade 150 machine. bash-3.00# showrev Hostname: u15_9 Hostid: 83685284 Release: 5.10 Kernel architecture: sun4u Application architecture: sparc Hardware provider: Sun_Microsystems Domain: sun.com Kernel version: SunOS 5.10... (1 Reply)
Discussion started by: kingston
1 Replies

8. UNIX for Dummies Questions & Answers

Exec explanation needed

Hello! I want to read a file line by line and have each line in a variable. I have found the following code. #!/bin/bash exec 3< data while read <&3 do echo "The number is $REPLY" a.out "$REPLY" done exec 3>&- I don't understand the use of exec and its arguments, though having read... (3 Replies)
Discussion started by: myle
3 Replies

9. Shell Programming and Scripting

sed command explanation needed

Hi, Could you please explain me the below statement -- phrase wise. sed -e :a -e '$q;N;'$cnt',$D;ba' abc.txt > xyz.txt if suppose $cnt contains value: 10 it copies last 9 lines of abc.txt to xyz.txt why it is copying last 9 rather than 10. and also what is ba and $D over there in... (4 Replies)
Discussion started by: subbukns
4 Replies

10. Shell Programming and Scripting

Perl and binary workings

Perhaps it's me - maybe I'm dumb and am getting this working solution wrong... I have a binary value 00000011 in $binVal and I want to print the result in denary, so in perl I did as the perldoc -f oct told me to do and added a 0b prefix as so: $binVal = "0b" . $binVal; then I wanted to... (5 Replies)
Discussion started by: WIntellect
5 Replies
Login or Register to Ask a Question