Bash script unable to disable expected output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script unable to disable expected output
# 1  
Old 05-03-2018
Bash script unable to disable expected output

I'm trying to understand why a script behaves different when run through a pipe.

My OS:

Code:
Linux  myip 3.13.0-92-generic #139-Ubuntu SMP Tue Jun 28 20:42:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

This script (myscript.sh):

Code:
#!/bin/bash

echo whoami:
whoami
echo who:
who
echo who am i:
who am i
echo logname:
logname

Gives the expected output of:

Code:
whoami
ubuntu
who
ubuntu   pts/1        2018-05-03 14:15 (8.21.68.241)
who am i
ubuntu   pts/1        2018-05-03 14:15 (8.21.68.241)
logname
ubuntu

But when run like through a pipe, the "who am i" command does not show the same output. doesnt show anything at all.

Code:
ubuntu@myIp:~$ cat ~/myscript.sh | bash
whoami:
ubuntu
who:
ubuntu   pts/1        2018-05-03 14:15 (8.21.68.241)
who am i:
logname:
ubuntu


What could be the problem? How can i get the piped way of running the script behave exactly as how the script would behave if run the regular way?
# 2  
Old 05-03-2018
Without a terminal you don't get the usual terminal control niceties. It does work, you just don't see it. Try redirecting the result into a file.
These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 05-03-2018
Expanding a little on what Corona688 has already said, the who utility uses its standard input file descriptor to determine the terminal being used as your controlling terminal. If your script is not the first process in a pipeline or if standard input is redirected from a non-terminal device file, it might not know how to determine who you are.

On a macOS or BSD system, these conditions would lead to output similar to:
Code:
dwc      tty??    May  3 09:59

instead of the normal output:
Code:
dwc      ttys007  Apr 28 08:53

that I get when standard input is connected to my terminal. I'm surprised that Linux (at least Ubuntu Linux) doesn't give you any output at all.
This User Gave Thanks to Don Cragun 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

Bash script does not work as expected

Repeat this text in a file named notes.txt and run the script Before bash is a good language a blank line appears Also, the following notes are displayed incorrectly What is bad? ================================== Title : Note 1 ================================== Category: Computer Date... (3 Replies)
Discussion started by: cesar60
3 Replies

2. Shell Programming and Scripting

Need help fix my script to get alerts when the command produce n expected output

Hi, Below is my script, which is used to invoke a test using curl command. #/usr/bin/sh MAILTO=user@xyz.com URL='https://myserver.xyz.net/test/dir/test123.tws' SOAPFILE=/tmp/soap.txt curl -k -s -S --header 'Content-Type: text/xml;charset=UTF-8' --data @"${SOAPFILE}" "${URL}" ... (3 Replies)
Discussion started by: System Admin 77
3 Replies

3. Shell Programming and Scripting

Getting error in bash script; expr $a + 1: integer expression expected

Hi, I am new to shell/bash script. I am trying to run below script #!/bin/bash a=0 b=10 if then echo "a is equal to be" else echo "a is not equal to be" fi MAX=10 while do echo $a a='expr $a + 1' done (1 Reply)
Discussion started by: Mallikgm
1 Replies

4. Shell Programming and Scripting

[Solved] Unable to call a python script from bash

Hi, I am trying to run a python script embedded in bash script. But is throwing me an error. Please help. Script: #!/bin/bash nohup /usr/bin/python /opt/web/http.py & Error: /usr/bin/python: can't open file '/opt/web/http.py': No such file or directory Please help me on this. (6 Replies)
Discussion started by: maddy26615
6 Replies

5. Programming

CGI Perl script to execute bash script- unable to create folder

Hi I have a bash script which takes parameters sh /tmp/gdg.sh -b BASE-NAME -n 1 -s /source/data -p /dest/data/archive -m ARC gdg.sh will scan the /source/data and will move the contents to /dest/data/archive after passing through some filters. Its working superb from bash I have... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

6. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

7. Shell Programming and Scripting

bash script error with binary operator expected.

Hello, I am not sure, where I am missing in the scirpt, I am trying to grep few users from /etc/passwd file and if exists, I added line to echo as user exist, if not create it. #!/bin/bash for vid in v707 z307 z496 z163 z292 ; do if then echo " $vid User exists " else ... (2 Replies)
Discussion started by: bobby320
2 Replies

8. Shell Programming and Scripting

Unable to change environment variables in bash script

Hello! For the moment some settings in my .bashrc contain the password of my company's firewall, which is not a good idea. I would like to use the string "PASSWORD" set in .bashrc and a script that changes all appearances of "PASSWORD" in the environment variables by the actual password (which... (4 Replies)
Discussion started by: markolopa
4 Replies

9. Shell Programming and Scripting

Bash Script: Trouble unable to run

I am trying to create a menu, and the script fails on ln 38 (Files in pwd). Any idea on where the problem is?? Thanks for the help Rob #!/bin/bash # Cool Script for Weekly Assignment 2 (#3) that creates a menu to act as a ui # and run some popular commands. clear while : do ... (9 Replies)
Discussion started by: rchirico
9 Replies

10. Shell Programming and Scripting

Bash script on startup does not respond as expected

Hi, I have a bash script which I have referenced in the rc.local of my fedora linux OS. However it doesnt respond the same as when run in terminal from fedora. The bash script has a series of interactive questions that require user input as shown: #!/bin/bash echo "Do you want to use... (1 Reply)
Discussion started by: Crigamorfin
1 Replies
Login or Register to Ask a Question