-2

I want to be able to display the content of my command-list document but whenever I do it just prints out "./commands.txt" but if I try the same thing outside of my script it works just fine.

This is my code:

    helpFile="./commands.txt"

if [ "$com" = "help" ]
then
cat $helpFile
fi
NanoCoder
  • 35
  • 1
  • 6
  • $com is a variable I use to determine what command the user has entered. What you're seeing on my post is just one command. By the way, it didn't work :/ – NanoCoder Dec 05 '15 at 22:26
  • 1
    Are you saying instead of the contents of `commands.txt`, you just see the literal output `./commands.txt` and no surrounding errors like "No such file or directory?" – that other guy Dec 05 '15 at 22:33
  • I use the read-command to set $com. – NanoCoder Dec 05 '15 at 22:34
  • Yes, exactly, that other guy. – NanoCoder Dec 05 '15 at 22:34
  • Have you tried this snippet on its own like @Cyclone suggests? If it's part of a larger script, the bug could be somewhere else entirely, and just not show up until this point. – that other guy Dec 05 '15 at 22:38

2 Answers2

2

I don't see where you get the $com variable from, but if you set it based on the first argument this should work:

#!/bin/bash

helpFile="./commands.txt"
com=$1

if [ "$com" = "help" ]
then
  cat $helpFile
fi

In the above example $com will be set to the first argument passed to the script, so if you would like to display the contents of ./commands.txt you would call it like ./<script.sh> help

I'm also thinking that you should check so the file really does exists in the current working directory or perhaps try to use an absolute path i.e:

helpFile="/home/commands.txt"
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
0

I found out what was wrong. My text editor screwed up and was saving all the new edited content on the desktop instead of the folder with the script and text file. Anyways, thanks for all your help guys, I really appreciate it :)

NanoCoder
  • 35
  • 1
  • 6