0

I want to run my code with nohup shellskriptname &. But whenever I try to see the nohup, it says there is no such file. What can I do, so I can enter it like that?

#!/usr/bin/bash
# To Display Files and Folders from current Directory

echo Displaying all the Directories in the current directory

for item in * do if [ -d $item ] then echo $item fi done

echo Displaying all the Files in the current directory

for item in * do if [ -f $item ] then echo $item fi done

Kusalananda
  • 333,661
Eronsee
  • 1
  • 2
  • Did you run chmod +x display_files.sh? Your script requires execute permissions. – Bib Aug 15 '22 at 17:37
  • Hello, yes it is -rwxrwsrwx – Eronsee Aug 15 '22 at 17:38
  • Then perhaps the CWD is not in the PATH, and it needs to be called with ./display_files.sh`. – Bib Aug 15 '22 at 17:40
  • Would you please edit your question to show the command you type where invoking the script works, and the nohup command you type where invoking the script fails? And also include the error message that you get from the failing command. – Sotto Voce Aug 15 '22 at 17:43
  • Where should I enter this in my code? – Eronsee Aug 15 '22 at 17:44
  • This is the code I type. On AIX on ksh it always works like this nohup display_files.sh & – Eronsee Aug 15 '22 at 17:45
  • @Eronsee are you on AIX or Linux? You've tagged with [tag:Linux] but you've mentioned AIX and ksh. – Chris Davies Aug 15 '22 at 17:46
  • 1
    I am currently on Linux – Eronsee Aug 15 '22 at 17:46
  • You should double-quote your variables when you use them. So instead of if [ -d $item ] you should write if [ -d "$item" ], etc. – Chris Davies Aug 15 '22 at 17:47
  • Also be aware that echo $item is not robust - consider a file called -n, for example. Instead, use printf '%s\n' "$item" – Chris Davies Aug 15 '22 at 17:48
  • Ok, but I think it doesn't solve the problem? running my code without the double-quotes works with ./display_files.sh. Do you agree it is a path problem? – Eronsee Aug 15 '22 at 17:49
  • I don't know. You haven't provided an exact error, just a paraphrase. For example it might have come from running the code. Pleaes provide the exact command you're running and the precise error message, and where you see it. – Chris Davies Aug 15 '22 at 17:50
  • works with your recommendation but not with nohup still :( ``` #!/usr/bin/bash

    To Display Files and Folders from current Directory

    echo Displaying all the Directories in the current directory

    for item in * do if [ -d "$item" ] then

                printf '%s\n' "$item"
        fi
    
    

    done

    echo Displaying all the Files in the current directory

    for item in * do

        if [ -f "$item" ]
        then
    
                printf '%s\n' "$item"
        fi
    

    done

    
    
    – Eronsee Aug 15 '22 at 17:53
  • Running like this: nohup display_files.sh & Error in the nohup.out: nohup: can't execute 'display_files.sh': No such file or directory – Eronsee Aug 15 '22 at 17:54
  • @Eronsee, if the current working directory is not in you path, then you need to give the path to the command, either relative or absolute. It's not nohup display_files.sh, it's nohup ./display_files.sh I think you are missing ./. – Bib Aug 15 '22 at 18:34
  • Eronsee please edit your question to include new information. It's way too easy to miss it in the comments – Chris Davies Aug 15 '22 at 19:19

1 Answers1

0

Whether you run your script with or without nohup, the shell will try to locate the named command (your script) in the directories listed in $PATH. It will do this unless you specify the name of your script with a directory path (either absolute or relative).

From the manual of nohup on my current FreeBSD system, regarding the utility's use of the PATH environment variable:

PATH
Used to locate the requested utility if the name contains no / characters.

This means nohup, just like the shell, would look up the given utility in $PATH if the command contains no directory path.

So, if your script, shellskriptname, is located in the current directory and if the current directory is not one of the directories in $PATH, then using just

shellskriptname

or

nohup shellskriptname

would fail in the way that you describe.

Instead, give the path to the script:

nohup ./shellskriptname

Add & at the end of the command if you need to run the command as an asynchronous job ("background job").

Regarding the actual script, please see When is double-quoting necessary?

Kusalananda
  • 333,661