file basename error ONLY with nohup - bash

I get an error when running the script (test.sh) as nohup. Interestingly, it works fine when ran as $./test.sh
Btw, the OS is X86 Solaris 10
$nohup ./test.sh &
./test.sh: syntax error at line 7: `file_name=$' unexpected
Here is the script test.sh
#!/usr/local/bin/bash
for dt in 20150806
do
stat_files="/storage2/Production_Stats/*"$dt"*"
for file_path in $stat_files
do
file_name=$(basename $file_path)
echo $file_name
done
done

Apparently, I had to use the deprecated back ticks `` and not the $() to make it work on Solaris 10. Thanks

Related

Syntax error when running bash script in Homestead Vagrant

I'm trying to run a script in Vagrant (vagrant ssh), however I am getting the error below:
tests/unit/runTests.sh: 4: tests/unit/runTests.sh: Syntax error: "(" unexpected
Command: sh tests/unit/runTests.sh
The script runs fine outside for vagrant and the only script info I've been able to find about vagrant are related to provisions, which is not what I'm doing in this case.
Script:
#!/bin/bash
i=0
fails=()
for d in tests/unit/*/ ; do
eval "vendor/bin/peridot -c tests/unit/peridot.php $d"
if [ $? -eq 1 ]
then
fails[$i]=$d
fi
wait
done
for ((j=0; j < ${#fails[#]}; j++)) do
echo "${fails[$j]}"
done
I've read about "(" syntax errors being related to dash, but I am using the suggestions that those posts had (#!/bin/bash).
Thanks for any suggestions.
Command: sh tests/unit/runTests.sh
It means that you (vagrant) is trying to run your script in 'sh', not in 'bash'. In this case, the first line (#!/bin/bash) is ignored and 'sh' interpret doesn't expect '(' character.

Bash functions inside process substitution

Getting an error trying to call a function inside a process substitution.
Is there any way to do this?
#!/bin/bash
function testfunc
{
echo "bork"
}
diff <(testfunc) <(echo "bork")
The error is:
bork.sh: line 7: syntax error near unexpected token `('
bork.sh: line 7: `diff <(testfunc) <(echo "bork")'
--Update--
Problem was calling sh bork.sh, instead of bash ./bork.sh . Moral of the story make sure which shell you are executing with.
There's no problem here:
$ chmod +x test.sh
$ ./test.sh
Clear diff. No problem!
$ bash -x ./test.sh
+ diff /dev/fd/63 /dev/fd/62
++ testfunc
++ echo bork
++ echo bork
Proof that it worked
Troubleshooting:
Maybe you
run in a restricted shell
you don't have /dev/fd available/mounted correctly (due to somekind of secure chroot jail?)
The problem is probably that you're running the command with sh instead of bash.
$ cat > xx.sh
#!/bin/bash
function testfunc
{
echo "bork"
}
diff <(testfunc) <(echo "bork")
$ sh xx.sh
xx.sh: line 7: syntax error near unexpected token `('
xx.sh: line 7: `diff <(testfunc) <(echo "bork")'
$ bash xx.sh
$
The process substitution is not portable to the shell in /bin/sh. See the Bash manual on POSIX mode and bullet 28:
Process substitution is not available.
Tested on Mac OS X 10.10.5 (Yosemite).

Executing a shell script from a file

My OS platform is this : SunOS machinehull01 5.10 Generic_148888-05 sun4v sparc SUNW,Sun-Fire-T200
I have written a shell script to run from a file
File name: test.sh
#!/bin/sh
VARNAME=$grep '-l' TestWord /home/hull/xml/text/*.txt
echo "Found $VARNAME"
When I run the above command in the console I'm getting the correct output without errors, But when I run sh test.sh or ./test.sh I'm getting below error
test.sh: -l: not found
Found
Can someone please help me on this?
You are searching for so called "command substitution" :
VARNAME=$(grep -l TestWord /home/hull/xml/text/*.txt)
echo "Found $VARNAME"
It will execute the command between $( and the closing parenthesis ) in a subshell and return the output of the command into VARNAME.
Got it.
#!/bin/sh
VARNAME=`grep -l TestWord /home/hull/xml/text/*.txt`
echo "Found $VARNAME"
I had to put those (`)there.

How can I resolve this error in shell scripting: "read: Illegal option -t"?

#!/bin/bash
echo -n "Hurry up and type something! > "
if read -t 10 response ; then
echo "Greate, you made it in time!"
else
echo "sorry, you are too slow!"
fi
I have written above code in terminal and got error "read: Illegal option -t".
Bash supports -t, so it looks like you're trying to execute it with sh or some other shell, which is odd, since you have the correct shebang.
Make sure you run it with ./script or path_to_script/script. If you just run it in the terminal, first start bash.
I had the same problem and then I figured out that I was using #!/bin/sh instead of #!/bin/bash. After changing the shebang everything worked as desired.
bash supports the -t option for the read builtin since version bash-2.04 (see ChangeLog), so either you are using an ancient version of bash (<= 2.03) or are not really running your script under bash.
Run bash --version to check the version and double-check that your shebang really looks like #!/bin/bash in your script.

bash for loop work in command line, but failed in script

When a run a for statement in debian bash command line, it works fine.
But when I run it in a sh script or run it with bash command, it's keeping report "error near unexpected token `do'"
Where is the difference?
[leon#www] ~/tmp $ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[leon#www] ~/tmp $ bash for i in {1..10}; do echo $i; done
-bash: syntax error near unexpected token `do'
BTW, all works fine in centos enviorment.
Use the -c option so that bash reads the commands from the string you pass in. Also, use single quotes around the command.
bash -c 'for i in {1..10}; do echo $i; done'
your bash command line ends with the first ;
so it gets executed separately as:
bash for i in {1..10};
do echo $i;
done
and man bash says command argument should be a file to load: bash [options] [file]
You can wrap all your script inside inverted commas or in a file. Because here, you're doing bash for i in {1..10} then do echo $i and so on. You should use -c option if you don't put it in a file.

Resources