LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-20-2015, 08:56 PM   #1
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 259

Rep: Reputation: 24
sh script - Literal Not Found


Script:
Code:
#!/usr/bin/sh
COUNT=0
NUMBER=$#

while [$COUNT -lt $NUMBER ]
do
    COUNT='expr $COUNT+1'
    TOKEN='$'$COUNT
    shift
done

echo $COUNT
Yields:
Code:
Shift.sh: line 5: [0: command not found
I'm invoking with:
sh Shift

.sh

The script is set to be executable.

It's the first script out of the 2003 version of Mastering UNIX Scripting by Randal Michael.
 
Old 12-21-2015, 12:04 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Even spaces have importance:
Code:
while [ $COUNT -lt $NUMBER ]; do
 
Old 12-21-2015, 03:29 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I would also expect that the quotes are actually back ticks?
 
Old 12-21-2015, 07:19 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
The fact that you can run it at all means at least that you have a /usr/bin/sh. For my system it needs to be changed to /bin/sh. Also thinking that this is more common. So I'm curious what does this command result in?
Code:
ls -l /usr/bin/sh
Fix the space in the while loop as NevemTeve says and you'll get it running, however you're incrementing COUNT incorrectly. Try:
Code:
((++COUNT))
 
Old 12-22-2015, 07:03 AM   #5
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 259

Original Poster
Rep: Reputation: 24
Well, changing the ' to ` resolved one issue.
Removing the `$` resolved the other.

I now understand the difference between a single-quote and the back tick.

My while functioned as expected.

Thank you, all, for the help.
 
Old 12-23-2015, 11:27 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For arithmetic, use (( )) format as above, instead of your backtick stuff.

For calling a cmd generally, use $(cmd) instead of backticks anyway (unless the shell won't allow it). Its a lot more readable and nestable if you really need it. Personally I would avoid nesting if possible.
 
  


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
How do I write a literal (type char) in C? stf92 Programming 12 07-03-2012 08:02 AM
how to pass an arg containing a literal '$'? BrianK Linux - General 5 04-27-2006 11:21 PM
too literal! treebug Linux - General 2 03-16-2006 12:30 PM
python cant assign to literal master Programming 4 09-05-2004 04:36 PM
How do you set a shell script var with a literal '*'? BrianK Programming 3 06-22-2004 02:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:55 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