Command behaves different in script and on prompt


 
Thread Tools Search this Thread
Operating Systems AIX Command behaves different in script and on prompt
# 1  
Old 08-16-2010
Command behaves different in script and on prompt

Code:
$cat /tmp/tuxob.lst
udi *****
jim 10
ant 19
ibm *****

$ input=`head -1 /tmp/tuxob.lst | awk '{print $NF}'`
$ echo $input

The output I am expecting is '*****'. But It is showing me the available files of current directory. When I run the command
head -1 /tmp/tuxob.lst | awk '{print $NF}
from unix prompt - I can see the output as '*****'

Please advise, what am I doing wrong?

Last edited by Scott; 08-16-2010 at 06:20 AM.. Reason: Please use code tags
# 2  
Old 08-16-2010
inside your script the variable input will have ****** as value.

and when you echo $input that evaluates to echo ******.

echo * will print the current directories content.

that is what is happening. try to use escape *.
# 3  
Old 08-16-2010
Code:
$cat /tmp/tuxob.lst
udi *****
jim 10
ant 19
ibm *****

Code:
input=`head -1 /tmp/tuxob.lst | awk '{print $NF}'`

If above is the script, then how do I put if conditions. i.e. If $input = '*****' do something. Because on putting $input displays files of current directory being expanded to echo *

edit by bakunin: i added the CODE-Tags you probably have forgotten.

Last edited by bakunin; 08-17-2010 at 06:25 AM..
# 4  
Old 08-17-2010
Quote:
Originally Posted by panchpan
Code:
input=`head -1 /tmp/tuxob.lst | awk '{print $NF}'`

If above is the script, then how do I put if conditions. i.e. If $input = '*****' do something.
First off, you don't need any if-contructions. You should quote your variables regardless of them containing something which has to be quoted or not, because even if said variables normally don't have a content which has to be quoted they might do so occasionally and a script shouldn't work "most of the times", but always.

In programming caution sometimes helps but never hurts, so be as cautious as possible.

Secondly, you shouldn't use the backticks any more: they are supported only for backwards compatibility purposes. Use the "$(...)" instead:

Code:
input="$(head -1 /tmp/tuxob.lst | awk '{print $NF}')"

Thirdly, the command only fills a variable with some content - in this case some asterisks. No shell expansion is done here. It is probably done when you subsequently try to display the variables contents. THIS is the place where you have to protect your variable from being expanded by the shell:

Code:
input="$(head -1 /tmp/tuxob.lst | awk '{print $NF}')"
echo $input         # will be expanded
echo "$input"       # will not be expanded

Btw.: if the only thing you want to do is to display it, you don't even need the variable:

Code:
echo "$(head -1 /tmp/tuxob.lst | awk '{print $NF}')"  # will not be expanded
echo $(head -1 /tmp/tuxob.lst | awk '{print $NF}')    # will be expanded

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script not working but command works in command prompt

Hi everyone I have a problem with my script If I try directly this command /usr/bin/nice -n 19 mysqldump -u root --password="******" wiki_schneider -c | nice -n 19 gzip -9 > /point_de_montage/$(date '+%Y%m%d')-wiki-db.sql.gz It works But if I simply add this command in a script and... (8 Replies)
Discussion started by: picemma
8 Replies

2. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

3. Shell Programming and Scripting

su - user -c 'command' behaves differently

I notice that su - user (note with dash) brings in more of the user's environment than does su - user -c 'command'. For example, if root does an su - user, and types "umask" to the prompt, one umask is displayed; yet, if instead the command is su - user -c 'umask', the value is different. I thought... (2 Replies)
Discussion started by: drokerm
2 Replies

4. Shell Programming and Scripting

Executing a script from CRON behaves differently than terminal

Hi have a script which transferers from Microsoft server to Linux box. The scripts(ksh) is on Linux box. If I run script from terminal, it transfers files to directory. Where as If I run script from CRON. It does not. Here is the log of both: Terminal execution log:... (2 Replies)
Discussion started by: dipeshvshah
2 Replies

5. Shell Programming and Scripting

script submitted using ssh does not return to command prompt

Gurus, I have written a shell script to backup a database and applications. This is how the script works: 1. As applications user (say applmgr), the main backup script, say backup.sh, is kicked off. 2. backup.sh does ssh to oracle user and kicks off the database backup using nohup and... (1 Reply)
Discussion started by: sunpraveen
1 Replies

6. UNIX for Dummies Questions & Answers

running command prompt executable file in shell script

hi i have file extentioned with test.vbs. i am able to run this file n execute through command promt but i dont know how to run in shell script example: file name is test.vbs which contains strSoundFile = "C:\windows\Media\Notify.wav" Set objShell = CreateObject("Wscript.Shell") strCommand... (5 Replies)
Discussion started by: atl@mav
5 Replies

7. Windows & DOS: Issues & Discussions

Awk script in DOS and Linux behaves differently :(

Hi, I have an awk script which performs simple operations of variable assignments and finally printing the variables in custom form. BEGIN {FS=OFS="\n"} { v1=substr($0,1,15) v2=substr($0,16,200) v3=substr($0,216,20) print v1 "|" v2 "|" v3 } The input file being processed... (2 Replies)
Discussion started by: vidyak
2 Replies

8. Shell Programming and Scripting

Script behaves different when run from cron vs. manually

Hey all, Just wanted to get some input on a script I am using to import files into a MySQL database. The process is pretty simple: my main server exports these files and FTPs them. I have a script that FTPs them to the machine running that runs this script. The FTP script runs without issue... (2 Replies)
Discussion started by: billtwild
2 Replies

9. Shell Programming and Scripting

Why Does Command Run From Prompt But Not From Script?

I hope someone can shed any light on this mystery. I am trying to run the following command: su userID -c remsh server -l userid -n "awk -F^ '\$4 == \"SMITH\"' /tmp/infromational/version74b/LIVE/TEMPORARY/ABCfiles/HLC_Database_File.bat|head -1" > /tmp/variant/45BV32/var/store13.logfnd I... (15 Replies)
Discussion started by: Korn0474
15 Replies

10. Shell Programming and Scripting

Command prompt from within a script?

I am writing a menu script and one of the options is to access a Command Prompt and return to the menu via CTRL-D. I have tried using a loop and echoing the PS1 value, then using read to assign whatever is entered into a variable, then executing the value of that variable as a command using .... (1 Reply)
Discussion started by: sanitywonko
1 Replies
Login or Register to Ask a Question