System date to unix variable

I want to run the following script

#!/bin/sh temp =`date +%Y%m%d` echo "$temp" 

But this does not work as expected, it throws this error message: execution permission denied

+4
source share
5 answers

You have

 temp =`date +%Y%m%d` ^ 

So you need to remove the space between temp and date :

 #!/bin/sh temp=$(date +%Y%m%d) # better than `exp`, thanks @OliverDulac (see comments) echo "$temp" 

With this he works for me.

Make sure the file has execute permissions:

 chmod +x your_file 

or just do it with

 /bin/sh /your/script/path/your_file 
+8
source

temp variable must be assigned without spaces.

0
source

Your script does have a syntax error to fix this by removing the space after temp in the second line, but this error will not throw execute permission denied , but line 2: temp: command not found .

Your script does not have execute rights to fix it:

 chmod +x FILE.sh 

where FILE is the name of this script.

0
source

In sh (not just bash) scripts, the equal sign is not accepted when assigning a variable, because you can do something like this:

 VARIABLE=something ./runcommand 

so that this variable is exported to a subprocess. / runcommand, but was not exported at all to all subprocesses.

If space were allowed, it would not be possible to distinguish between the end of the destination and the beginning of the command line.

0
source

I found an error in my previous code, I got a space after the temp line. what causes the error.

0
source

Source: https://habr.com/ru/post/1482919/


All Articles