Different output based on run method


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Different output based on run method
# 1  
Old 05-29-2015
Different output based on run method

Sample Data
Code:
#cat /mylist.txt
/raid/job/123
/raid/job/456
/raid/job/789
#

Script
Code:
#cat /myfind.scr
#!/bin/bash

touch /nojob.txt
touch /job.txt
unset dirfolder
for dirfolder in `/bin/cat /mylist.txt`;do
if [ -d "$dirfolder" ]
then
    echo $dirfolder >>/job.txt
else
    echo $dirfolder >>/nojob.txt
fi
done
#

The intention is to check and see if the directory exists, this runs on Red Hat 6. If the three jobs exist (which they should but that's besides the point) then it should put them in job.txt. If they do not exist then they should go in nojob.txt. If I run this line by line then it would end up correctly. If I run it like
Code:
/myfind.scr

Then it does just the opposite and all three go in /nojob.txt. Why is it behaving differently if I run it line by line compared to running the whole file? I'd like it to act the same either way.
# 2  
Old 05-29-2015
Hi crowman

What is that extension *.scr?
For scripts the extension usualy represents the shell its running, bash often been shortend to .sh as well.

It is a serious NO-GO to place scripts and data files in the system root.
Thats what /root, $HOME/.cache, /opt or /tmp is for, thought they're not necesarily better...

Note that you can place your scripts in $HOME/bin so you'll have them available whereever you are, as long you are the corresponding user.

Since i didnt see any error, i rewrote it.
In this script i've changed the used path/datadir to $HOME/data.
Code:
#!/bin/bash
DATA=$HOME/data
INPUT=$DATA/mylist.txt
JOB_NO=$DATA/nojob.txt
JOB_YES=$DATA/job.txt

[ -d "$DATA" ] || mkdir -p "$DATA"
touch $JOB_{YES,NO}

while read dirfolder
do	[ -d "$dirfolder" ] && \
		echo "$dirfolder" >> "$JOB_YES" || \
		echo "$dirfolder" >> "$JOB_NO"
done<"$INPUT"

Hope this helps
This User Gave Thanks to sea For This Post:
# 3  
Old 06-01-2015
Thanks Sea,

I've used
Code:
.scr

short for script regardless of the shell. Non programming people run these things sometimes and I've tried to simplify it for them. I'm not actually writing to root, I removed path names I didn't want to post. I guess I'll leave it a mystery what it acts differently run line by line compared to the whole thing. I'll try what you provided.

Thanks again Sea!
# 4  
Old 06-01-2015
scr is actualy a windows screensaver file extension.
Thats why i asked. Smilie

PS:
Some distros have .sh files associated with their default terminal, so a by double clicking (if it has execute flag) it will prompt to either open in terminal, editor or just execute it.
Naming it scr might prevent such a question, but idk.

hth

I didnt really change much, but i did assume a checkpath resolving error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

When I run the code yesterday I am getting output,when I run same code today I am getting error?

If run the below code today its creating all directory and getting output files,I f run same code tomorrow I am getting error. can any one give suggestion to sortout this error. OSError: no such file or directory : '062518'My code looks like this import paramiko import sys import os ... (8 Replies)
Discussion started by: haribabu2229
8 Replies

2. Shell Programming and Scripting

To run the script based on dates

I am having below script which needs to be executed based on start and end date #!/bin/bash array=('2016-09-27' '2016-10-27' '2016-11-27' '2016-12-27' '2017-01-27' '2017-02-27' '2017-03-27' '2017-04-27' '2017-05-27' '2017-06-27' '2017-07-27' '2017-08-27' '2017-09-27' ) for i in "${array}" do... (9 Replies)
Discussion started by: rohit_shinez
9 Replies

3. Windows & DOS: Issues & Discussions

Method to run a shell script using shortcut key in keyboard with out logging to putty

Dear All, I want to run a shell script with out logging to putty but configuring it to a keyboard short cut it windows PC. Can this be done? I want this to rename a log in a specified folder in a system Thanks (8 Replies)
Discussion started by: Chi_SL
8 Replies

4. Shell Programming and Scripting

Run this grep every 10 minutes and do something based on the output

OS : Red Hat Linux 6.4 Shell : Bash We have a file called status.txt which will have only 1 line. The content will be the string "Processing" for most of the day. # cat status.txt Processing #I want to write a shell script (notify.sh) which will be executing a grep every 10 minutes . ... (7 Replies)
Discussion started by: kraljic
7 Replies

5. Shell Programming and Scripting

Who -u gives different output if run from cron than if run from terminal?

If I run 'who -u' interactively or from a script invoked through bash in a tty on my Ubuntu 12LTS box I get an output like this: testuser pts/0 Dec 9 02:32 . 2163 (host.xx.yy) running the same through cron I get: testuser pts/0 2012-12-09 02:32 00:05 2163... (2 Replies)
Discussion started by: latimer
2 Replies

6. Shell Programming and Scripting

Read 2 lines from File, Run Command based off output

Okay, so I have a file containing line after line of three digit numbers. I need a script that does an action based on the last two numbers in this list. So.... To get the last two numbers, I can have the script do tail -2 filename.txt But where I run into trouble is as follows. If... (6 Replies)
Discussion started by: UCCCC
6 Replies

7. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

8. Programming

infinite loop, synchronizing gossip-based method

the following code runs, but it hangs somewhere, i don't know why, #include<iostream> #include<vector> #include<cstdlib> #include<ctime> #include<list> #include<pthread.h> #include<cstring> using namespace std; pthread_mutex_t listlock = PTHREAD_MUTEX_INITIALIZER;... (3 Replies)
Discussion started by: saman_glorious
3 Replies

9. Shell Programming and Scripting

Converting line output to column based output

Hi Guys, I am trying to convert a file which has a row based output to a column based output. My original file looks like this: 1 2 3 4 5 6 1 2 3 1 2 3 (8 Replies)
Discussion started by: npatwardhan
8 Replies

10. Programming

Not able to execute the method based on OS in JAVA

Hi, In my application,I want to find out the OS name and based on OS I want to do some specific operations.So I write some code based on it in JAVA but I am able to get the OS name but Not able to use the method specific to OS.I write a simple code to check it and found that it is not going to... (1 Reply)
Discussion started by: smartgupta
1 Replies
Login or Register to Ask a Question