LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-06-2017, 09:15 AM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Rep: Reputation: Disabled
Question bash script: "echo" being missed when script is executed


My installed shellcheck passes it as having no errors.
Is it my choice of variables? I've used a few, and here is the latest script.
Code:
#!/bin/bash
c="$1"
mapfile -t honest<"$c"
fg=${honest[0]}
if [[ ! -f $fg ]]; then
 echo -e "File $fg is not in this directory.\nCan't continue."
 exit 0
fi
cap1=${honest[1]}
caplen=${#cap1}
comm1=${honest[2]}
comlen=${#comm1}
k=${honest[3]}
kk=$(echo "$k" | tr ',' ' ' | wc -w)
echo "$kk"
echo "All the lines look right. Here goes."
echo "READING information FROM $c"
echo "Caption is $caplen characters long (spaces included)."
echo "Comment is $comlen characters long (likewise)"
echo -e "$fg: There are $kk keywords."
Line 20, (echo -e "$fg: There are $kk keywords."), works when executed from stdout, but not during execution. What could be wrong?

Carver
 
Old 11-06-2017, 09:35 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
bash shell has a built in "echo" command.

Separately there is a binary command called "echo" typically /bin/echo.

The "-e" is valid for the binary but not for the built in.

Change your command line to the path of the echo command e.g.
Code:
/bin/echo -e "File $fg is not in this directory.\nCan't continue."
and
Code:
echo -e "$fg: There are $kk keywords."
OR you could set a variable at start of script:
Code:
ECHO=/bin/echo
Then use that:
Code:
$ECHO -e "File $fg is not in this directory.\nCan't continue."
and
Code:
$ECHO -e "$fg: There are $kk keywords."
 
Old 11-06-2017, 09:36 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,735

Rep: Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920
I would start with debugging your script. You can add set -x at the beginning of your script as well as adding extra echo commands at other places in your script to verify variables are being assigned as desired.


http://tldp.org/LDP/Bash-Beginners-G...ect_02_03.html
 
Old 11-06-2017, 12:49 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
Replace echo (non-portable) with printf (portable and a shell built-in).
Especially if you use variables with unknown contents; e.g. a leading -n or -e in the variable contents can mess it up. In contrast, in printf all formatting is done by the first argument, and the variables are safely put into the further arguments.
Code:
kk=$(printf "%s\n" "$k" | tr ',' ' ' | wc -w)
Code:
printf "%s: There are %s keywords.\n" "$fg" "$kk"
Code:
 printf "File %s is not in this directory.\nCan't continue.\n" "$fg"

Last edited by MadeInGermany; 11-06-2017 at 12:55 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"ps" command not executed in script when using crontab Annielover Linux - General 10 08-10-2018 01:00 AM
[SOLVED] Shell Script: "bash: Bad Substitution - Script for remove "." " thiagofw Programming 14 12-09-2016 10:04 PM
[SOLVED] Bash - Capture "Child" script output with "Parent" without using files or coproc keyword Jason_25 Programming 2 02-14-2016 07:51 PM
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
[SOLVED] executed konsole from script does not "inherit" functions sharky Linux - Newbie 2 01-25-2012 12:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration