Unexpected token with Dialog command - dialog

The dialog box as shown in https://bash.cyberciti.biz/wiki/index.php?title=A_progress_bar_(gauge_box)&mobileaction=toggle_view_mobile does not work. Essentially I am trying to make a progress bar with a for command using dialog.
Each variation of the substitution does not to seem to work for the second example and I am unable to figure out why.
dialog --title "Copy file" --gauge "Copying file..." 10 75 < <(
Code goes here
)
Actual Results: syntax error near unexpected token `<'

The issue ended up being that it was using sh ./filename.sh to execute as opposed to bash.

Related

No Error from Script for Non-Existent File

I have a shell script that reads a text file and uses its content. So far so good. But now I'm trying to make the script exit if the file is not found. The script looks like this
#!/bin/bash
function errorcatcher() {
errorcode=$?
echo "ERROR CODE : ${errorcode}"
exit ${errorcode}
}
trap errorcatcher ERR
MYFILE=$1
IFS='|'
while read line; do
echo ${line}
done < ${MYFILE}
echo "Execution complete"
And I run the script as
sh myscript.sh /home/mydir/ABC.txt
and it works fine. But if I try this
sh myscript.sh /home/mydir/nonexisting.file
I get
myscript.sh: line 17: /home/mydir/nonexisting.file: No such file or directory
Execution complete
Function errorcatcher does not get invoked and instead of exiting with an error code, the execution continues and I get the line Execution complete even though the file in question doesn't exist. My guess is no error is generated here, so I added this line before reading the text file
ls ${MYFILE}
The errorcatcher gets invoked this time. But if I try
sh myscript.sh /home/mydir/ABC.tx
Instead of existing file ABC.txt, I pass its incomplete name ABC.tx and again, the errorcatcher function is not invoked and the script completes successfully (Execution complete gets echoed).
Could someone help me with this? I'm curious as to why errorcatcher doesn't get invoked
for a non existing file without ls
for incomplete file name (ABC.tx) with ls
Function errorcatcher does not get invoked …
Indeed, with an error in the redirection of a loop like
while read line; do
…
done < ${MYFILE}
the ERR trap is not invoked. You have discovered an undocumented exception in the implementation of the trap command, or, if you prefer, a bug.
You can evade that by adding an additional test of the redirection before the while, e. g. the line
<$MYFILE
on its own will invoke the error trap.

Can't populate variable from command in Bash

I'm new to bash scripting, and I'm working on a script where the user enters a username and gets a list of the associated information from /etc/passwd. Unfortunately, I seem to be having trouble populating a variable from a command. The error message I'm getting suggests the if statement isn't being entered into, but I'm not sure why.
The script currently looks like this:
#!/bin/bash
#readifs
FILE=/etc/passwd
read -p "Enter a username > " user_name
file_info=$(grep "^$user_name:" $FILE)
if [ -n "$file_info" ]; then
IFS=":" read user pw uid gid name home shell <<< "$file_info"
echo "User = '$user'"
echo "UID = '$UID'"
echo "GID = '$GID'"
echo "Full Name = '$name'"
echo "Shell = '$shell'"
else
echo "No such user '$user_name'" > &2
exit 1
fi
When I run it, using a valid username, I get the following two lines:
readifs.sh: line 20: syntax error near unexpected token `&'
readifs.sh: line 20: ` echo "No such user '$user_name'" > &2'
I'm pretty sure I'm missing something obvious, or doing something bash doesn't allow but I'm too new to catch. Can anyone point out and correct the error in my script?
Thank you to Charles Duffy for all the great feedback on not just this script, but bash scripting and Stack Overflow in general.
I was able to fix the script as I wanted. I removed the ^ and : from the file_info line, which was stopping the grep command from finding the line I wanted. I also renamed $UID and $GID to use lower case letters, and removed the space in "> &2".
Thank you again for your assistance.

I am skeptical about the redirection operation of below example

I was successful in redirecting the error message to a text file in the below process:
$ ls + 2>err.txt
$ cat err.txt
ls: cannot access +: No such file or directory
But when I try to attempt the same process with echo command it shows different output and unable to redirect the error message to a text file.
$ echo )hey 2>err.txt
bash: syntax error near unexpected token `)'
In your first example, it is the ls command that produces the error message that is written to err.txt.
In your second command, you are expecting the following to happen:
The line is successfully parsed
bash opens err.txt for the standard error of echo
echo tries to output )hey
echo encounters an error
The error message is written to err.txt.
However, bash never makes it past the first line, so none of 2 through 5 ever happens. Instead, the shell immediately stops processing the line and prints the error message to its own standard error.

Shell script - multiline statements work from command line (PuTTy) but not from script file

(I apologize in advance: I don't know whether my problem concerns rather the code syntax or the file system and would - in the latter case -rather fit into a Linux forum)
I'm trying to set up a litte shell script. But as soon as it comes to multiline statements, I came across a strange behaviour.
It took the example blow from a tutorial page:
number=1
if [ $number = "1" ]; then
echo "Number equals 1"
else
echo "Number does not equal 1"
fi
That works fine if I connect via PuTTy to my virtual linux machine (openSUSE 13.1) and copy&paste the code. It does what is expected.
But when I create a file named shell_test.sh (connected via SFTP Net Drive) containing the content below
#!/bin/bash
number=1
if [ $number = "1" ]; then
echo "Number equals 1"
else
echo "Number does not equal 1"
fi
and call it from the command line with bash shell_test.sh I get an error:
line 7: syntax error near unexpected token `fi'
The same happens with a for loop. The syntax error is then near the token "do".

How do I fix unexpected end of file in bash script?

I had much luck last time I submitted a question so here goes: I am trying to debug a somewhat large BASH script when I get the following error:
./test.sh: line 418: unexpected EOF while looking for matching `"'
./test.sh: line 427: syntax error: unexpected end of file
The code below starts at line 400:
echo "###########################################################"
echo
;;
4)
culebra_carriers
get_month
get_day
logs_cdrs
logs_wap
get_mdn
echo
echo "###########################################################"
echo
echo "Searching for activity of $mobileNumber on $MON $DAY......."
echo
zgrep $mobileNumber $HOME/culebrapeak/$LOGCDR/$CULEB/$MON/$WAPLOG
echo
echo "###########################################################"
echo
;;
esac
done
}
clear
main_menu
How do I make this error go away? It looks like I have the double quotes in all the right places... but this is only my 4th or 5th bash script... so please go easy on me.
I was, indeed, missing a double quote at the top of my script. Thanks to all for the help!
A good way to solve problems like this is to use a text editor that highlights code between quotes. Short of that, if the "find" feature of your text editor gives a count too, you may be able to use it to quantitatively detect start/end character symmetry problems. The highlighting from the find feature will aide your eye tremedously.

Resources