0

I have a problem with executing bash script on Android or TWRP. I use BusyBox 1.34.1 with Termux application. The command that I use is du. All the time when I use variable with path in script I receive the error Inaccessible or not found.

When I use static path I got the correct values:

Script:

#!/bin/bash

mainLocation="/data/media/0/!Temp/!Test"

echo "Rozmiary katalogow:"
du -hd 1 /data/media/0/!Temp

Executing:

sh script3.sh    
: inaccessible or not found                     
: inaccessible or not found                     
Rozmiary katalogow:                             
697M    /data/media/0/!Temp

But when I use the path from variable I got the error with no values:

Script:

#!/bin/bash

mainLocation="/data/media/0/!Temp"

echo "Rozmiary katalogow:"
du -hd 1 "$mainLocation"

Executing:

sh script3.sh                  
: inaccessible or not found                     
: inaccessible or not found                     
Rozmiary katalogow:                             
: No such file or directory 

So where is the problem?

Michal
  • 61
  • 9
  • 1
    First of all, `!` is a special character in bash, it will get used for history expansion. You need to use single quotes for your variable or rename the folder. Second, you can always use `bash -x` or `echo` to check the contents of a variable. – mashuptwice Apr 21 '22 at 01:33
  • 2
    From the way those error messages are garbled, I'm pretty sure your script file has DOS/Windows line endings. See ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Gordon Davisson Apr 21 '22 at 01:40
  • @mashuptwice : But history expansion is only done if bash is intaractive, so it should not matter here. – user1934428 Apr 21 '22 at 05:38
  • 1
    Maybe not relevant for this problem, but if you run your script as `sh script3.sh`, it is not executed by bash, but - as the command states - by `sh`. This will bite you once you are using bash-specific features. For debugging the problem, I would run it by `bash -x script3.sh` and analyze the diagnostic output you get. Also (for the safe side) you may do a `echo $BASH_VERSION` inside your script, so that we can see which bash is executing it. – user1934428 Apr 21 '22 at 05:41
  • Guys, thanks! Now it works awesome. The problem was with DOS line endings. I used `dos2unix` command :) Thanks for the `bash -x script3.sh` command - it is very helpful! – Michal Apr 21 '22 at 19:15

0 Answers0