-1

i am trying to give an file as input to me my shell script:

#!/bin/bash
file ="$1"
externalprogram "$file"
echo 'unixcommand file ' 

i am trying to give the path to my file but it says always

cannot open `=/home/username/documents/file' (No such file or directory)

my path is this /home/username/Documents/file

i do this in terminal : ./myscript.sh /home/username/Documents/file

can someone help me please?

4 Answers4

0

When you say

file ="$1"

with a space after "file", you're running something called file with =$1 as an argument. There probably actually is a utility called file. If you want to assign $1 to a variable called file, you don't need the space:

file="$1"

Kevin Boone
  • 4,092
  • 1
  • 11
  • 15
  • 1
    No "probably" about it; `file` is a [standard utility](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/file.html). – chepner Sep 11 '20 at 19:21
  • It's reasonable to assume that _that_ `file` is the one being invoked here, but it's an assumption. Container-based Linux images may not include `file` or, in fact, any of the 'standard' utilities. – Kevin Boone Sep 13 '20 at 06:44
0

there shouldn't be a space before = in the second line. file=$1 should be good enough.

陈建伟
  • 1
  • 3
0

Check what shellcheck says about your code:

 ^-- SC1068: Don't put spaces around the = in assignments (or
 quote to make it literal).

You can read more about SC1068 case on its Github page.

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
0
#!/bin/bash

file=$1

code $file

echo "aberto o arquivo ${file} no vscode"

I made this code snippet to demonstrate, I pass a path and it opens the file in vscode

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197