Visit Our UNIX and Linux User Community


Command works interactively but not in bash script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Command works interactively but not in bash script
# 1  
Old 11-13-2019
Command works interactively but not in bash script

The below command works in the terminal interactively but not as part of a bash script. I though maybe I needed to escape the "$dir" so it isn't interpreted literally, but that's not it. Thank you Smilie.

interactively in terminal

Code:
dir=/path/to
new=$(ls "$dir"/*.csv -tr | tail -n 1) && echo "the current csv is from: " && date -r "$new" "+%m/%d/%Y"

the current csv is from: 
10/31/2019

as part of #!/bin/bash

Code:
the current csv is from:

# 2  
Old 11-13-2019
Hi,

I'm not getting the same error, although I've tested running the shell from the same directory as the test data and a different directory;

Is there more to the script - see below for the test.

Code:
fbakirpomd5 (root) /root-> pwd
/root
fbakirpomd5 (root) /root-> ls *.csv
file_o1.csv
fbakirpomd5 (root) /root-> cat test.sh
#!/bin/bash
dir=/root
new=$(ls "$dir"/*.csv -tr | tail -n 1) && echo "the current csv is from: " && date -r "$new" "+%m/%d/%Y"
fbakirpomd5 (root) /root-> ./test.sh
the current csv is from:
05/14/2019
fbakirpomd5 (root) /root-> /usr/local/bin/test.sh
the current csv is from:
05/14/2019
fbakirpomd5 (root) /root->

Regards

Gull04
This User Gave Thanks to gull04 For This Post:
# 3  
Old 11-13-2019
Please post operating system details.

Can you show output from :
Code:
which bash
head -1 <your script> | od -cb

Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 4  
Old 11-13-2019
Quote:
Originally Posted by cmccabe
The below command works in the terminal interactively but not as part of a bash script. . .
Difficult to believe. Any error messages? is dir exported / defined (within the script)?


Try also
Code:
stat -c"the current csv is from: %y" *.csv | sort -r -k6,6 | head -1

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-14-2019
I found the error in the script... thank you Smilie.
This User Gave Thanks to cmccabe For This Post:
# 6  
Old 11-14-2019
After 1300ish posts one should know to post error you found.
This will benefit others and be in search results.

So, please, post the solution.

Regards
Peasant.
These 2 Users Gave Thanks to Peasant For This Post:
# 7  
Old 11-14-2019
Maybe this will help others:

In $dir the were multiple csv files that were the same name, so I needed to add a condition to remove all but the last modified that matched the file I was looking for.

awk -F, '/Date/ {print $2}' "$new" (extracted the current) that I compared to the date -r "$new" "+%m/%d/%Y" (these had to match) then the $new was unique and the script worked. Thank you Smilie.

Last edited by cmccabe; 11-14-2019 at 04:34 PM.. Reason: added details

Previous Thread | Next Thread
Test Your Knowledge in Computers #638
Difficulty: Easy
The original iPhone had 16GB of memory.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Expect scripting issue, works interactively when doing commands in cli, does not work in script

Hi; problem may be obvious, simple but I have to say it is somehow not easy to locate the issue. I am doing some word extracting from multiline text. Interacting in CLI seems to work without issues. First step is to add multiline text to a variable. expect1.1> expect1.1> set... (2 Replies)
Discussion started by: aldowski
2 Replies

2. Shell Programming and Scripting

Cp command works on command line but not in bash

The below command moves all the .vcf files into the directory. cp /home/cmccabe/Desktop/test/vcf/overall/stats/*.vcf /home/cmccabe/Desktop/NGS/annovar When I use a bash wrapper the target.txt gets created but the text files do not get copied. All the paths are the same, but not sure why... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Script in bash that works only some of the time

I ran this script yesterday (in the background) /usr/bin/nohup myfilelocation/myscriptname.sh & the script worked perfectly. i ran it today (also in the background) and just sat there. So i killed it and ran it normally and it worked perfectly. Anyone suggest why it just sat there and... (8 Replies)
Discussion started by: twinion
8 Replies

4. Shell Programming and Scripting

Calling bash script works when called manually but not via Cron?

Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin. Basically the path structure is ... (6 Replies)
Discussion started by: wyclef
6 Replies

5. UNIX for Dummies Questions & Answers

Bash script dont works when executed as cronjob

Hello, i have cronjob: crontab -l * * * * * pkill -f domexpcheck;sh /root/dom/domexpcheck.sh it runs: /var/log/cron Mar 25 12:11:01 vps crond: (root) CMD (pkill -f domexpcheck;sh /root/dom/domexpcheck.sh) but somehow script dont run properly via cronjob. But when i execute cronjob... (7 Replies)
Discussion started by: postcd
7 Replies

6. UNIX for Dummies Questions & Answers

C-program works fine interactively, but not on the SGE server

Greetings, I have a C-program that is made to implement a hidden Markov model on an input file. The program is very memory intensive. I've installed it on my local server where I have an account and it compiles fine. The way they have the server set up is that you can either work... (1 Reply)
Discussion started by: Twinklefingers
1 Replies

7. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

8. Shell Programming and Scripting

Script works with bash 3.0 but not 3.2.

Hello, So my knowledge of bash scripting is not that great and I have been trying to solve this problem on my own for awhile to no avail. Here's the error I get when running it with an OS that uses bash 3.2.x: testagain.sh: line 10: *-1: syntax error: operand expected (error token is... (2 Replies)
Discussion started by: forkandspoon
2 Replies

9. Shell Programming and Scripting

Works from bash prompt, but not from script!

I'm trying to use unison from bash on windows with cygwin. I don't know if this is a cygwin question, bash question or unison question. Since I always get reprimanded by the cygwin mailing list for assuming it is a cygwin problem, I'll assume it is a bash question. The following commands work... (7 Replies)
Discussion started by: siegfried
7 Replies

10. Shell Programming and Scripting

substring command works but only in BASH shell

I am having trouble running a .sh file. The code 'x=${file_name:0:$z-11}' is giving me a bad substitution error. However when I run in BASH it works. Thing is when this goes to production the .sh will not be running in BASH. Is there a way to substring a string not in BASH or a way to invoke... (2 Replies)
Discussion started by: edwardtk11
2 Replies

Featured Tech Videos