Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 412  / 1 Year ago, fri, february 17, 2023, 5:52:51

I am studying Bash, but there are some things that are not explained in my book. First I'll post a script, and then I will ask questions going through scripts.



Bash Script:



$ cat sortmerg
#!/bin/bash
usage ()
{
if [ $# -ne 2 ]; then
echo "Usage: $0 file1 file2" 2>&1
exit 1
fi
}

# Default temporary directory
: ${TEMPDIR:=/tmp}

# Check argument count
usage "$@"

# Set up temporary files for sorting
file1=$TEMPDIR/$$.file1
file2=$TEMPDIR/$$.file2

# Sort
sort $1 > $file1
sort $2 > $file2

# Open $file1 and $file2 for reading. Use file descriptors 3 and 4.
exec 3<$file1
exec 4<$file2

# Read the first line from each file to figure out how to start.
read Line1 <&3
status1=$?
read Line2 <&4
status2=$?

# Strategy: while there is still input left in both files:
# Output the line that should come first.
# Read a new line from the file that line came from.
while [ $status1 -eq 0 -a $status2 -eq 0 ]
do
if [[ "$Line2" > "$Line1" ]]; then
echo -e "1. $Line1"
read -u3 Line1
status1=$?
else
echo -e "2. $Line2"
read -u4 Line2
status2=$?
fi
done

# Now one of the files is at end-of-file.
# Read from each file until the end.
# First file1:
while [ $status1 -eq 0 ]
do
echo -e "1. $Line1"
read Line1 <&3
status1=$?
done
# Next file2:
while [[ $status2 -eq 0 ]]
do
echo -e "2. $Line2"
read Line2 <&4
status2=$?
done
# Close and remove both input files
exec 3<&- 4<&-
rm -f $file1 $file2
exit 0


Questions:




  1. First of all, how do I indent codes in Gedit? I copied and pasted codes from a book, and it doesn't indent the codes automatically. Do you normally use Gedit or any other editor popular for scripting bash?


  2. : ${TEMPDIR:=/tmp}

    Can you explain what this is? I have programming knowledge of C# and other programming languages. So can you tell me, assuming I'm not total newbie at programming, what the : key and the curly bracket keys do?


  3. if [[ "$Line2" > "$Line1" ]]; then

    [] is same as test. But why does [[]] do any different?


  4. status1=$?

    what is $? ?




Many thanks in advance.


More From » bash

 Answers
6

  1. I use vim for writing shell scripts.


  2. ${TEMPDIR} will expand to the value of the variable named TEMPDIR. ${TEMPDIR:=/tmp} will do the same, but if it's empty (or unset), the value /tmp will be assigned to TEMPDIR, and also expanded.



    Having ${TEMPDIR:=/tmp} alone on a line, will cause it to be changed to e.g. /tmp which will try to execute /tmp as a command (which will obviously fail, since you can't execute a directory). That's why the : (null) command is used. The null command ignores all input, all arguments, and does absolutely nothing. Run help : to see the description of that builtin command.



    See http://mywiki.wooledge.org/BashFAQ/073 For the various things you can do with Parameter Expansion.


  3. [[ "$line2" > "$Line1" ]] returns true if line2 sorts after line1 (like strcmp in C).



    [("test" command) and [[ ("new test" command) are used to evaluate expressions. [[ works only in Bash, Zsh and the Korn shell, and is more powerful; [ and test are available in POSIX shells.



    See http://mywiki.wooledge.org/BashFAQ/031 for the dfference between the [ command and the [[ keyword.


  4. ? is a special parameter that holds the exit status of the last command executed. $? expands the value of that parameter.




On a side note, if that's an example from your book, I'd say that's a poor source for learning bash. I recommend reading http://mywiki.wooledge.org/BashGuide which also teaches good practices.


[#29089] Saturday, February 18, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
otatorm

Total Points: 218
Total Questions: 113
Total Answers: 124

Location: British Indian Ocean Territory
Member since Tue, Feb 22, 2022
2 Years ago
otatorm questions
;