Read command not working as expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read command not working as expected
# 8  
Old 02-27-2012
Methyl, it is not the same problem as the previous one.
In the previous case, the script was executed from cygwin terminal in win xp environment. But now I was trying to run the script from linux terminal in Ubuntu env.
I guess the problem might be due to some shell related or setting problem. As I am new to linux, can't figure out the exact problem.
I will post the output of the command once I execute the command in my ubuntu laptop. Thank you.
# 9  
Old 02-27-2012
How are you calling the script? If you call it for example as sh ./script then the shebang is ignored and you are using /bin/sh , which is dash on Ubuntu, not bash....
# 10  
Old 02-27-2012
I was calling the script as sh scriptname
# 11  
Old 02-27-2012
Right, then that explains it. So you would need to use ./scriptname or bash ./scriptname..
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 02-27-2012
@linux_learner
When posting a problem please state what Operating System and version you are running and what Shell you are using. Every time.
When you have a problem please post the script intact (blotting anything confidential with X's) and any error messages verbatim.
If there is a question about different environments please post the output from the command set for both the environment which works and the environment which does not work. Just doing this will often make the solution obvious to you. Sometimes a cron containing just set will reveal the problem.

Back on topic.
Srutinizer has cracked it. You ran a "bash" Shell in a "dash" environment. This was unguessable based on the information originally posted.
Now if you had posted the exact error messages verbatim?

The main lesson here is that "bash" has many extensions to scripting language which are really useful but which do not work at all in normal unix Shells.

When you get an error message, post the error message 100% accurately case-significant and with every space character intact.
There is no Shell which outputs "no such commands" or "invalid option with the command" but there are some which output "syntax error" but probably not in lower-case.
This User Gave Thanks to methyl For This Post:
# 13  
Old 02-27-2012
Thanks methyl and Scrutinizer, now it is clear to me and it is working as expected.

@Scrutinizer, could you please explain me how to check which shell is being used when I am running some script in Ubuntu? If I run the command echo $SHELL, it displays /bin/bash, so I thought the script was run in bash shell.

Thanks in advance.
# 14  
Old 02-28-2012
Hi linux_learner, sh script means that /bin/sh will be used to execute the script, overriding any shell that is specified on the first line in the script that starts with #!
Historically /bin/sh was the Bourne Shell, on modern systems /bin/sh usually means a POSIX shell, with a limited set of features of what bash offers. So if you use sh, you should limit yourself to the POSIX feature set. On many linuxen /bin/sh this is a link to /bin/bash and if bash gets called this way it conforms to POSIX more closely, but not fully ( for example read -n still works ). On Ubuntu /bin/sh is linked to /bin/dash, which is a very fast shell that is pretty near a POSIX shell and not much else..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Why this script is not working as 'expected' when doing ssh with while read ... really confused?

Hi, I have a script below that is running ssh <host> <command> on some servers. Below is more or less the script. I have to modify it somehow to get rid of the 'confidential' hostnames check_log.bash #!/bin/bash # myPID=$$ parse_log () { sub="parse_log" host=${1} ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

3. Shell Programming and Scripting

awk command not working as expected

Following one line of awk code removes first 3 characters from each line but when I run the same code on another linux platform it doesn't work and only prints blank lines for each record. Can anyone please explain why this doesn't work? (31 Replies)
Discussion started by: later_troy
31 Replies

4. Shell Programming and Scripting

Cp command not working as expected in HPUX

Hi, I'm having trouble with a simple copy command in a script on HPUX. I am trying to copy a file and append date & time. The echo command prints out what I am expecting.. echo "Backing up $file to $file.$DATE.$FIXNUM" | tee -a $LOGFILE + echo 'Backing up... (4 Replies)
Discussion started by: Glennyp
4 Replies

5. Shell Programming and Scripting

ash busybox read command not working inside function....

I have a script that has to execute a read command in a function, this is in an ash busybox. The code is... trapcatch () { echo "Ctl-c Detected, what do you want to do?" echo "Please choose the number of one of the following options" echo "1. Jump past this Set" echo "2. Exit... (8 Replies)
Discussion started by: tesser
8 Replies

6. UNIX for Dummies Questions & Answers

-atime not working as expected

I need to sort through a volume that contains video files by access time and delete files that have not been accessed over x days. I have to use the access time as video files are originals that do not get modified, just read Testing commands on a local test folder... $ date Wed Sep 28... (10 Replies)
Discussion started by: canon273
10 Replies

7. Shell Programming and Scripting

Why this is not working in expected way?

total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $totalThis outputs: 1 3 6 10 15 0whereas I am expecting: 1 3 6 10 15 15My bash version: (4 Replies)
Discussion started by: meharo
4 Replies

8. OS X (Apple)

Cat command not working as expected

I've been trying to figure this out since last night, and I'm just stumped. The last time I did any shell scripting was 8 years ago on a Unix box, and it was never my strong suit. I'm on a Mac running Leopard now. Here's my dilemma - hopefully someone can point me in the right direction. I'm... (10 Replies)
Discussion started by: Daniel M. Clark
10 Replies

9. UNIX for Dummies Questions & Answers

Find command not working as expected

I have a script with a find command using xargs to copy the files found to another directory. The find command is finding the appropriate file, but it's not copying. I've checked permissions, and those are all O.K., so I'm not sure what I'm missing. Any help is greatly appreciated. This is... (2 Replies)
Discussion started by: mpflug
2 Replies

10. Shell Programming and Scripting

which not working as expected

Hello. Consider the following magic words: # ls `which adduser` ls: /usr/sbin/adduser: No such file or directory # Hmmm... Then: # ls /usr/sbin/adduser /usr/sbin/adduser # Now what? Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)
Discussion started by: osee
2 Replies
Login or Register to Ask a Question