LinuxQuestions.org
Review your favorite Linux distribution.
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 01-25-2018, 01:10 AM   #1
pankajk2526
LQ Newbie
 
Registered: Jan 2018
Posts: 5

Rep: Reputation: Disabled
Smile Bash error 1: command not found


There is an Error in
Code:
ch=$ch$($i)
in the following code:

Code:
#!/bin/bash

origclip="open documents open music"
i=0
set -- $origclip
ch=$1


while [[ ch != "" ]]; do
    ((i++))
    ch=$ch$($i)                      #this does not execute even ch=$($i) doesn't execute
    echo $ch
done
I want to achieve following output.

Code:
open
open documents
open documents open
open documents open music
new line and space are not required, presence of space don't matter in output.

In short I want to achieve output of following code, with a variable 'i' which increments as in previous code

Code:
origclip="open documents open music"
set -- $origclip
echo $1
echo $1$2
echo $1$2$3
echo $1$2$3$4
Also there is a problem in achieving end of line
Code:
while [[ ch != "" ]]; do
i am not sure of this.

Last edited by pankajk2526; 01-25-2018 at 02:27 AM.
 
Old 01-25-2018, 02:05 AM   #2
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,830

Rep: Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033
While I'm no expert in scripting, the "command not found" error message means that the shell (bash) cannot find the executable for that command.

I'd say it's in the following line where your problem lies:

Code:
while [[ ch != "" ]]; do
Probably inside those brackets somewhere.

I've asked for this question to be moved to the Programming forum, so you can get better answers.
 
Old 01-25-2018, 02:13 AM   #3
pankajk2526
LQ Newbie
 
Registered: Jan 2018
Posts: 5

Original Poster
Rep: Reputation: Disabled
The problem lies here:

Code:
origclip="open documents open music"
set -- $origclip
ch=$1
echo $ch                                #this executes well
i=2
ch=$ch$($i)                             #this gives error 2: command not found
The last line in above code will not execute in terminal
even last line in below code will not execute, which is simpler

Code:
origclip="open documents open music"
set -- $origclip
ch=$1
echo $ch                                #this executes well
i=2
ch=$($i)                                #this gives error 2: command not found

Last edited by pankajk2526; 01-25-2018 at 02:24 AM.
 
Old 01-25-2018, 02:14 AM   #4
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,830

Rep: Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033Reputation: 2033
Just noticed the following:

Code:
#!/bin/bashorigclip="open documents open music"
should be:

Code:
#!/bin/bash
origclip="open documents open music"
up the top there.
 
Old 01-25-2018, 02:18 AM   #5
pankajk2526
LQ Newbie
 
Registered: Jan 2018
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jsbjsb001 View Post
Just noticed the following:

Code:
#!/bin/bashorigclip="open documents open music"
should be:

Code:
#!/bin/bash
origclip="open documents open music"
up the top there.

No, that was just a typing error in this post, i have edited it now,.... please consider this while answering the question...
Code:
origclip="open documents open music"
set -- $origclip
ch=$1
echo $ch                #this executes well
i=2
ch=$($i)                #this gives error 2: command not found

Last edited by pankajk2526; 01-25-2018 at 02:23 AM.
 
Old 01-25-2018, 02:30 AM   #6
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,766

Rep: Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577
Doing the substitution for the last line gives
Code:
ch=$(2)
which clearly shows where your error message is coming from.

Likely you want
Code:
ch=${$i}

Last edited by descendant_command; 01-25-2018 at 02:32 AM.
 
Old 01-25-2018, 02:34 AM   #7
pankajk2526
LQ Newbie
 
Registered: Jan 2018
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by descendant_command View Post
Doing the substitution for the last line gives
Code:
ch=$(2)
which clearly shows where your error message is coming from.

Likely you want
Code:
ch=${$i}
That results in error bash: ${$i}: bad substitution
 
Old 01-25-2018, 03:03 AM   #8
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,766

Rep: Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577
Yeah, I thought that might happen.

You have further research to do on nesting bash variables, but at least you're not trying to execute a command called '2' any more ...
 
Old 01-25-2018, 03:07 AM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,581
Blog Entries: 11

Rep: Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596
It is not clear what your actual goal is, but that is a hard way to go about getting what you have described.

You may have some reason for setting the values of positional parameters, but if not something like this will do what you have described:

Code:
origclip="open documents open music"
for wd in $origclip; do
        ch="${ch}${wd}"
        echo "$ch"
done
The reason for the error in your example code is this...

Code:
ch=$ch$($i)                      #this does not execute even ch=$($i) doesn't execute
$($i) attempts to execute $i, so the command does not exist. Were you thinking that this was equivalent to an array subscript?

The problem with detecting the end of the string is that you are testing whether "$ch" is empty, when you continue to append more to it - so it is never empty.

You need to read up on how shell scripting works.

I highly recommend the bash man page as an excellent place to get the details - well organized and well written (uncommon among man pages!).

You might also like to look over the Bash Guide For Beginners, and the Advanced Bash Scripting Guide.

Last edited by astrogeek; 01-25-2018 at 03:17 AM. Reason: Added end of line comment
 
1 members found this post helpful.
Old 01-25-2018, 04:52 AM   #10
pankajk2526
LQ Newbie
 
Registered: Jan 2018
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thankyou!

I can't believe you solved this... answer accepted...
 
Old 01-27-2018, 01:11 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,675

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
Yes, $($i) runs the contents of i as a command. Not wanted here.
For a space-separated list a for loop is more appropriate than a while loop.

Code:
origclip="open documents open music"
set -- $origclip

ch=""
for c; do
    ch=$ch$c
    echo "$ch"

done
The for takes the positional parameters as default.
Perhaps you do not even need to set the positional parameters?
This was suggested before. Here is how to add a space separator in the output.
Code:
origclip="open documents open music"

ch="" sep=""
for c in $origclip; do
    ch=$ch$sep$c
    echo "$ch"
    sep=" "
done

Last edited by MadeInGermany; 01-27-2018 at 01:12 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Keep on getting error message Bash Command Not Found cpatte7372 Linux - Newbie 10 05-08-2017 03:58 AM
bash: command not found error Revenge7 Linux - Newbie 20 06-17-2015 08:33 AM
please help to fix the bash command not found error in tinyos mahewish Linux - Newbie 6 09-26-2012 06:52 AM
[SOLVED] Centos 5 Error :- -bash: yum: command not found sachinsud Linux - Server 56 06-21-2012 07:41 PM
bash: command not found error ksgill Linux - Newbie 10 07-01-2003 03:47 PM

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

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