My script doesn't work in the terminal window!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting My script doesn't work in the terminal window!
# 1  
Old 02-21-2013
My script doesn't work in the terminal window!

I am inexperienced with scripting and it continues to baffles me speechless

I wrote a script so that it counts the number of subset of files (with different prefix) in all subdirectories under the current directory so that

Code:
find ./ -type d -maxdepth 1 -mindepth 1 > list_of_dir.txt

find all subdirectories under the current directory and output the list to txt file

Code:
sed 's/^...//' <list_of_dir.txt >list_of_dir2.txt

edit the output file so that .//directory becomes directory

Code:
rm list_of_dir.txt

Code:
echo list of data > list_of_data.txt

Code:
for i in {1..$(wc -l < list_of_dir2.txt)}
do
echo "\n$(sed -n "$i"p list_of_dir2.txt)" >> list_of_data.txt
cd ./"$(sed -n "$i"p list_of_dir2.txt)"
ls *.jpeg | awk -F_ '{A[$1 FS $2]++} END {for (j in A) printf "%-20s%4d \n", j, A[j] >> "../list_of_data.txt"}' 
cd ..
done

from each subdirectory in the txt file, output the directory name to file list_of_data, then cd into that directory, then count all the number of subset of files under different prefixes and output the result to list_of_data.txt

The problem is, if I run the script by typing ./scriptname.sh I get the error message

Code:
sed: 1: "{1..23}p": invalid command code .
sed: 1: "{1..23}p": invalid command code .
ls: cannot access *.jpeg: No such file or directory

but if I copy the content of the script and paste it in the terminal window it worked.

sample output

directoryname
directoryname_1 10
directoryname_2 5

directoryname2
directoryname2_1 20
directoryname2_2 30

etc etc

Why is this happening?

Last edited by vbe; 02-21-2013 at 12:50 PM..
# 2  
Old 02-21-2013
How does your script start?
# 3  
Old 02-21-2013
the first line of code is how the script start
# 4  
Old 02-21-2013
Code:
{1..$(wc -l < list_of_dir2.txt)}

This does not work. You cannot put a command there.

Your other commands look like they are using sed and the like to extract a single line per loop. This is extremely poor practice.

Please explain what you are trying to do here.
# 5  
Old 02-21-2013
Thanks for your reply. As I said I am inexperienced I am sure there are much better way to write this.

Can you explain why the code works if I paste it in the terminal but it doesn't if I run it as a script?

As I explained above there are many subdirectories under the current directory. And inside each subdirectory there are a large number of files under different prefixes, like pict_1_1, pict_1_2, pict_2_1, pict_2_2, pict_2_3 etc etc etc

I want to count the number of file under each prefix from every subdirectory. with output like the sample output above into a txt file.

I would also appreciate it if you explain why you meant the extreme poor practice. I am keen to learn.
# 6  
Old 02-21-2013
If a script lacks a #! first line it is sent to sh not bash. See man execvp. Man Page for execvp (opensolaris Section 0) - The UNIX and Linux Forums
# 7  
Old 02-22-2013
I think I am beginning to understand what the problem is. It seems the problem occurs with the sed command in the for loop. the error message
Code:
sed: 1: "{1..23}p": invalid command code .

stems from the fact that there are 23 folder in current directory and maybe I don't truely understand the mechanism of the for loop.....as
Code:
for i in {1..23}

then the
Code:
sed -n "$i"p list_of_dir2.txt

command tries to execute sed {1..23} all at the same time, hence the error.
During the for loop is it not like for i =1 then carries out the commands, then for i=2, carries out the command again etc etc until i=23? Why does sed trying to interpret i=1..23 all at the same time?

Still don't understand why the command runs when typed in terminal window but didn't when ran as script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Toggle between xterm window and standard terminal window

Is it possible to toggle back and forth between an xterm invoked from one tty, and a shell invoked from a different tty? I am running Centos 7 with KDE and booting in non-graphic mode. After logging in on the default window (/dev/tty1) , I can then use ALT-F2 to access a new window (/dev/tty2),... (1 Reply)
Discussion started by: rhgscty
1 Replies

2. Shell Programming and Scripting

[Solved] Script doesn't work..help?

hi, i am trying to run this script.the name of script is final.sh after i run it: #./final.sh & i grep the command # ps -a | grep bash and i see more then one processes runing 3!! Please use code tags how can i solve this problem? my target script must always run in... (8 Replies)
Discussion started by: zigizag
8 Replies

3. UNIX for Dummies Questions & Answers

Script run everytime a new terminal window is opened

I created a script called title #!/bin/sh echo "^0;$*^G" It will change the terminal window titlebar to what ever I type after the script (title BIG would change titlebar to BIG instead of terminal) Is there a way to make it run so it will work on every terminal window that gets opened.... (1 Reply)
Discussion started by: amason0508
1 Replies

4. Shell Programming and Scripting

my script doesn't work :(

i have this script and when i ejecute it, the console tell me this " sintax error line 41 unexpected element "}" " is the sintaxis ok? #!/bin/bash if ;then { exit 0; } if ; then { sudo /etc/init.d/apache2 start; sudo /etc/init.d/mysql start; php5 & nautilus... (3 Replies)
Discussion started by: keiserx
3 Replies

5. Shell Programming and Scripting

Script doesn't work in loop but does if not

I have a script that only works if I remove it from the looping scenario. #!/bin/bash # Set the field seperator to a newline ##IFS=" ##" # Loop through the file ##for line in `cat nlist.txt`;do # put the line into a variable. ##dbuser=$line echo "copying plugin..." ... (6 Replies)
Discussion started by: bugeye
6 Replies

6. Shell Programming and Scripting

two grep in one script doesn't work?

Hi there, the following script doesn't work. the first part works, then the second 'grep' fails with ': not found'. However, if I take out the second part (starting with the grep command) and put in a seperate script, it works. everyone know what's wrong here? no two 'grep' in one script, that... (2 Replies)
Discussion started by: monkey77
2 Replies

7. Shell Programming and Scripting

Help with script.. it Just doesn't work

Hello,, Im verry new to scripting and have some problems with this script i made.. What it does: It checks a directory for a new directory and then issues a couple of commands. checks sfv - not doing right now checks rar - it checks if theres a rar file and when there is it skips to... (1 Reply)
Discussion started by: atmosroll
1 Replies

8. Shell Programming and Scripting

gcd.sh script doesn't work...

Hi there. I'm new to scripting in bash shell and I have this problem. I'm trying to make a script that returns the greatest common divisor of two integer numbers according to Euclid's algorithm... Here is, what I've done: #!/bin/bash m=$1 n=$2 while do if ; #line 8 then m=$m-$n... (1 Reply)
Discussion started by: kantze
1 Replies

9. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

10. Solaris

Script to launch terminal window?

Hi, I am a newbie here. Trying to find a way of writing a script to launch multiple terminal or console windows on solaris 9. I used to be able to do this using cmdtool on older versions of solaris and it was even possible to configure the size and screen position of the window and the title. ... (5 Replies)
Discussion started by: omerta
5 Replies
Login or Register to Ask a Question