LinuxQuestions.org
Visit Jeremy's Blog.
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 05-07-2011, 02:47 PM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 443

Rep: Reputation: 21
Can't run some bash scripts


Could someone kindly explain to me why rt.sh and test.sh cannot be found, but randm3u.sh runs just fine.
Code:
g  ./rt.sh
: No such file or directory
g  ./test.sh
: No such file or directory
g  ls -l rt.sh test.sh randm3u.sh
-rwxrwxrwx 1 g users 1227 May  7 19:15 randm3u.sh
-rwxrwxrwx 1 g users  116 Apr 30 18:09 rt.sh
-rwxrwxrwx 1 g users  126 May  7 18:21 test.sh
g  ./randm3u.sh
This terminal is playing music. Type q to quit, spacebar to pause/unpause, f next, d previous, b restart tune
g
Following is the code for each script:
rt.sh
Code:
#!/bin/bash

# This script makes test.sh executable and runs it
cd /home/g/Scripts
chmod +x test.sh
./test.sh
exit 0
test.sh
Code:
#!/bin/bash 

# This script opens rxvt terminal and starts music playing
rxvt -e /home/g/Scripts/./randm3u.sh &
exit 0
randm3u.sh
Code:
#!/bin/bash

# generate a playlist of 50 randomly selected tunes from 3.n3u, and play them.
e=`cat /home/g/Documents/3.n3u`	# read pseudo-playlist into $e
rm /home/g/Documents/rand.m3u	# delete previous playlist
v=""
limit=${e: -7:5}	# the highest selection number in 3.n3u
oamt=0
d=
FLOOR=10000	# the lowest selection number in 3.n3u
while [ $oamt -lt 50 ]	# oamt controls generation of 50 tunes in rand.m3u
  do
    number=0	# set number so while loop executes at least once per cycle
    while [ "$number" -lt $FLOOR ]
      do
      		number=$RANDOM
      		let "number %= $limit"		# 10000 <= number < limit
      done
 		if [[ "${v}" =~ ${number} ]]; then			# if this number previously generated
 			continue								# do not select it again, all must be unique
 		else
 			grep -m 1 " $number" /home/g/Documents/3.n3u | awk '{ print $2 >> "/home/g/Documents/rand.m3u" }'
 			oamt=$((oamt+1))
 			v="$v $number"			# add number to list of unique numbers generated
 		fi
  done
echo This terminal is playing music.  Type q to quit, spacebar to pause/unpause, f next, d previous, b restart tune
mpg123 -b 2048 --preload 1 --smooth -C -@ /home/g/Documents/rand.m3u > /home/g/Documents/mpg123.dat 2>&1 		# play rand.m3u
exit 0
 
Old 05-07-2011, 03:07 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
The most obvious solution would be that there is an error in the contents of those scripts. In both cases, they are trying to execute commands in /home/g/Scripts
The final script is not using that path.
 
Old 05-07-2011, 03:59 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 443

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by smoker View Post
The most obvious solution would be that there is an error in the contents of those scripts. In both cases, they are trying to execute commands in /home/g/Scripts
The final script is not using that path.
By elimination, I traced the problem to the line
./test.sh
in rt.sh, and found that if I used
source test.sh
instead, then it worked as a command in the console. So I changed rt.sh to match, with this bizarre result
Code:
g  source rt.sh
: command not found
: command not found
: numeric argument required
root  chmod 600 test.sh
chmod: cannot access `test.sh': No such file or directory
root
Despite the 3 error messages rt.sh successfully started test.sh, which started randm3u.sh in its own terminal, which started mpg123 playing my tunes. Yet by some unknown means this caused the user g to be switched to root, so the command
chmod 600 test.sh
failed as I was no longer in the same directory. There is no su command in any of those scripts, no password was entered, yet it has changed to root. I have no clue as to why source works but ./ does not, nor why it should produce these bizarre side-effects.
 
Old 05-08-2011, 02:58 AM   #4
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 5.4, Mac OS 10.4 (tiger)
Posts: 1,005

Rep: Reputation: 79
quick question, is there actually any point to rt.sh? All it appears to be doing is making test.sh executable and running it... could you not simply just run test.sh directly in this case?

Also on the line "rxvt -e /home/g/Scripts/./randm3u.sh &" why have you got a ./? This means current directory so the ./ is redundant. From the directory that these scripts are in, do you mind just doing a pwd and checking that the path (including case) matches correctly. Also when calling scripts have you checked if "sh <script>" works?
 
1 members found this post helpful.
Old 05-08-2011, 10:40 AM   #5
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 443

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by r3sistance View Post
quick question, is there actually any point to rt.sh? All it appears to be doing is making test.sh executable and running it... could you not simply just run test.sh directly in this case?
As much as any other reason, I write scripts for their remembrance of things I forget. As a recent convert from windows I'm constantly stumbling over linux file permissions and whether or not something is currently executable, and rt.sh takes care of the latter.
Quote:
Also on the line "rxvt -e /home/g/Scripts/./randm3u.sh &" why have you got a ./? This means current directory so the ./ is redundant. From the directory that these scripts are in, do you mind just doing a pwd and checking that the path (including case) matches correctly.
Did that because I've been using "./" as shorthand for "source", and as I was having rxvt start a script I couldn't decide whether it was needed at all, whether I should put it at the beginning of the path, or where I did put it. Thank you for settling that question.
Quote:
Also when calling scripts have you checked if "sh <script>" works?
That's the first I've heard of sh, Slackware has it but no man page for it. But trying it out it seems the best of ./, source and sh.

./ won't run a script unless its executable.
source runs it whether or not it is executable, but closes out the terminal when done.
sh acts like source, but keeps the terminal open.

So rt.sh is redundant, and thank you again for all the info you've given me here, very useful to me.
 
Old 05-09-2011, 01:58 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
./x.sh = run script named x.sh in curr dir. It's NOT the same as 'source'; that would be '. ./x.sh'
'source' or '. ./' means inc code found in x.sh file(!) into the curr env/shell, therefore the x.sh file itself does not need +x perms.

sh creates a new (POSIX shell) env; you could just as easily call bash ..... note that sh != bash != ksh etc ...
 
Old 05-09-2011, 07:34 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 806

Rep: Reputation: 186Reputation: 186
I am assuming that the "g" in the command line is the present working directory So you are executing from "g" which is presumably /home/g.

But test.sh is in the folder /home/g/Scripts. So when you run the line ./test.sh it fails.
 
Old 05-09-2011, 11:57 AM   #8
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 443

Original Poster
Rep: Reputation: 21
I've had mark this thread as unsolved again. Yesterday I had the problem of some scripts not working, which was solved by starting them with sh. Shutdown last night and restarted this morning to find that now none of my scripts will run, regardless of how I try to start them. This is the script test.sh that I used for this trial.
Code:
#!/bin/bash 

/usr/bin/echo "does this even work" >> /home/g/script.log
exit 0
This is what I entered in the terminal, rxvt under fluxbox on Slackware 13.1
Code:
g  cd Scripts
g  ls -l test.sh
-rw------- 1 g users 85 2011-05-09 15:12 test.sh
g  ./test.sh >  /home/g/script.log 2>&1
g  sh test.sh >> /home/g/script.log 2>&1
g  source test.sh >> /home/g/script.log 2>&1
This is the output to script.log
Code:
bash: ./test.sh: Permission denied
test.sh: line 2: 
: command not found
test.sh: line 4: exit: 0
: numeric argument required
bash: 
: command not found
bash: exit: 0
: numeric argument required
Yesterday the system was producing comments like this but as the script ran successfully anyway, I just ignored them. Looks like I can't do that. Could someone explain what is going on here?
 
Old 05-09-2011, 01:02 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Please post the output of od -a test.sh
 
Old 05-09-2011, 01:20 PM   #10
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Somewhat off topic, but I think you could simplify your code if you replaced the "create a random list of fifty tunes stuff" with a simple shuf -n 50 -o /home/g/Documents/rand.n3u /home/g/Documents/3.n3u See man shuf for details.

<edit>
Also, as a general policy, I avoid using system command names as script names. While test.sh is not the same as the test command name, I, personally, find it better to avoid using names with "test" in them. (I burned myself with a "test.c" program followed by a "gcc -o test test.c" and much head scratching when the executable seemed to run, but did nothing . . .)
</edit>

Last edited by PTrenholme; 05-09-2011 at 01:29 PM.
 
Old 05-09-2011, 02:32 PM   #11
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 443

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by catkin View Post
Please post the output of od -a test.sh
g od -a test.sh
0000000 # ! / b i n / b a s h sp cr nl cr nl
0000020 / u s r / b i n / e c h o sp " d
0000040 o e s sp t h i s sp e v e n sp w o
0000060 r k " sp > > sp / h o m e / g / s
0000100 c r i p t . l o g cr nl e x i t sp
0000120 0 cr nl cr nl
0000125
g
 
Old 05-09-2011, 02:50 PM   #12
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 443

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by PTrenholme View Post
Somewhat off topic, but I think you could simplify your code if you replaced the "create a random list of fifty tunes stuff" with a simple shuf -n 50 -o /home/g/Documents/rand.n3u /home/g/Documents/3.n3u See man shuf for details.
Not that simple; if it were I'd use the -z option in mpg123. rand.m3u is a playlist, its source, 3n3u, is not, it includes a weighting system to make the tunes I really like come up much more frequently than those I just sort of like. I switched to this method a number of years back when I had to change players for the 3rd time because my playlist grew too large for the player to handle.
 
Old 05-09-2011, 06:23 PM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by porphyry5 View Post
g od -a test.sh
0000000 # ! / b i n / b a s h sp cr nl cr nl
0000020 / u s r / b i n / e c h o sp " d
0000040 o e s sp t h i s sp e v e n sp w o
0000060 r k " sp > > sp / h o m e / g / s
0000100 c r i p t . l o g cr nl e x i t sp
0000120 0 cr nl cr nl
0000125
g
And here's how it should look:
Code:
0000000   #   !   /   b   i   n   /   b   a   s   h  sp  nl  nl   /   u
0000020   s   r   /   b   i   n   /   e   c   h   o  sp   "   d   o   e
0000040   s  sp   t   h   i   s  sp   e   v   e   n  sp   w   o   r   k
0000060   "  sp   >   >  sp   /   h   o   m   e   /   g   /   s   c   r
0000100   i   p   t   .   l   o   g  nl   e   x   i   t  sp   0  nl
Looks as if it was created on a Windows systems with those "cr nl"s where they should be only "nl"s. Solutions listed here.
 
1 members found this post helpful.
Old 05-09-2011, 10:55 PM   #14
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 443

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by catkin View Post
And here's how it should look:
Code:
0000000   #   !   /   b   i   n   /   b   a   s   h  sp  nl  nl   /   u
0000020   s   r   /   b   i   n   /   e   c   h   o  sp   "   d   o   e
0000040   s  sp   t   h   i   s  sp   e   v   e   n  sp   w   o   r   k
0000060   "  sp   >   >  sp   /   h   o   m   e   /   g   /   s   c   r
0000100   i   p   t   .   l   o   g  nl   e   x   i   t  sp   0  nl
Looks as if it was created on a Windows systems with those "cr nl"s where they should be only "nl"s. Solutions listed here.
All these scripts were created on this linux machine, so that should not be true. But I went ahead and used fromdos on test.sh anyway, and found that it didn't work, it hung up in a loop. Now I had already used fromdos a couple of days ago, to convert 3.n3u (which did come from my windows machine), and it worked just fine. So I searched test.sh for \r\n in jedit, and it could not find the cr nl combination. Same with kwrite, but they both agree every line ends with nl. Ran od on it again and it still says they're both there. So I think perhaps od reports other values as cr, besides cr itself, but I have to look at the file in a hex editor to see what is really there. Strangely, Slackware 13.1 has neither khexedit nor shed. Given all the compilers and assemblers included by default, you'd think there'd be a hex editor somewhere, but I'll have to look for one on the net and install it.
 
Old 05-09-2011, 11:10 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
To cvt from MS -> *nix I use dos2unix http://linux.die.net/man/1/dos2unix
In vi/vim try
Code:
:set list
to see ctrl chars
 
1 members found this post helpful.
  


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
How can i run two bash scripts at same time? helptonewbie Programming 3 09-05-2006 07:25 PM
How to run Bash scripts like thread in Java? nguyenzoro Linux - General 1 11-11-2005 03:08 AM
Cannot run install sh scripts, and bash is FUBAR...what to do? asdfjkl Linux - Software 5 11-20-2004 10:09 PM
How to Run perl scripts using bash Boudewijn Linux - Software 2 11-26-2003 04:37 AM
bash scripts won't run aamehl Linux - General 10 09-06-2003 03:25 PM


All times are GMT -5. The time now is 09:48 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration