Why won't cat work inside my bash-script? - bash

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

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"

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 :)

Related

Is it possible to use scp and change a filename to $date

So I have this script that creates a file called "myfile.txt" this name will never change.
I have 2 servers srv01 and srv02
srv01:/home/user/scriptdata/myfile.txt is the file I want to move to srv02
I want this file to arrive at srv02 in the following format
srv02:/home/user/scriptoutput/$date.txt
Is there an easy way to do this?
So far I have been using scp /home/user/scriptdata/myfile.txt 12.34.56.78:/home/user/scriptoutput/day-month-year.txt manually
scp /home/user/scriptdata/myfile.txt 12.34.56.78:/home/user/scriptoutput/data_$(date +%d-%m-%Y_%H:%M).txt
This worked! thanks jhnc for ur answer

Replace string path in log file with current folder name

I'm new here and already tried to find solution to the following requirement without success. I'm trying to achieve this:
I have these 5 folders:
ServiceEngine
PaymentEngine
InvoiceEngine
ProcessEngine
OrderProcessEngine
Inside each of these folders, I have a log file with default path location to store the log files e.g. ServiceEngine/logs
The log file contain the following path structure:
name="RollingRandomAccessFile" fileName="logs/engine.log"
filePattern="logs/engine-%i.log"
I expect to find a way that I retrieve the name of the current folder which I'm in and replace the string engine with folder name
Example: I'm in ServiceEngine folder and execute a command that retrieve the current folder name. The expected result is:
name="RollingRandomAccessFile" fileName="logs/ServiceEngine.log"
filePattern="logs/ServiceEngine-%i.log
Later I change the directory to PaymentEngine and the expected result is:
name="RollingRandomAccessFile" fileName="logs/PaymentEngine.log"
filePattern="logs/PaymentEngine-%i.log
and so on. Maybe there is a smarter way to create a script that update the string in a loop like do; if ... fi; done or to use the for in ... loop.
Do you mean something like this?
~/ServiceEngine$ cat logfile
name="RollingRandomAccessFile" fileName="logs/engine.log"
filePattern="logs/engine-%i.log"
~/ServiceEngine$ awk -v path=$(basename $(pwd)) 'gsub("engine", path)' logfile
name="RollingRandomAccessFile" fileName="logs/ServiceEngine.log"
filePattern="logs/ServiceEngine-%i.log"
See basename, declaring variables in awk and awk gsub.
I guess I don't understand your question, but:
dir=$( basename $( pwd ) )
echo name="RollingRandomAccessFile" \
fileName="logs/$dir.log" \
filePattern="logs/$dir-%i.log"
Is that what you're looking for?
It sounds like you are looking for something like this:
$ sed 's/\bengine\b/'$(basename $(pwd))'/' logs
When run from within one of your folders, it spits out the text you're asking for. It wasn't clear what you wanted to do with that text though.

How do I make this if then else script

I've been trying to make this if then else script but its wrong every time.
I am brand new at this, maybe I have taken more than I can chew but I am trying to make a script that runs at launch that does this
If file exists /Users/Bob/Library/Safari/Bookmarks.plist
then do nothing
if file does not exist ( i assume this is "else") then move file from /Library/Application\ Support/Bookmarks.plist to /Users/Bob/Library/Safari/Bookmarks.plist
Any feedback is greatly appreciated, thanks in advance.
What you described could be written like that in pseudo code:
if file_exists("/Users/Bob/Library/Safari/Bookmarks.plist")
exit
if not file_exists("/Users/Bob/Library/Safari/Bookmarks.plist")
move("/Library/Application\ Support/Bookmarks.plist", "/Users/Bob/Library/Safari/Bookmarks.plist")
As you can see, the first "branch" is not really used, as you anyway plan to ignore this case.
But if we assume, that in the first case, you actually want to do something, like print a nice message, you could write it like that:
if file_exists("/Users/Bob/Library/Safari/Bookmarks.plist")
print("Nothing to do!")
else
move("/Library/Application\ Support/Bookmarks.plist", "/Users/Bob/Library/Safari/Bookmarks.plist")
As you can see, in the this example, the "else" part is equivalent to saying "if not file_exists(...)". You could also reverse the order by using "not":
if not file_exists("/Users/Bob/Library/Safari/Bookmarks.plist")
move("/Library/Application\ Support/Bookmarks.plist", "/Users/Bob/Library/Safari/Bookmarks.plist")
else
print("Nothing to do!")
DST="/Users/Bob/Library/Safari/Bookmarks.plist"
SRC="/Library/Application\ Support/Bookmarks.plist"
if [ ! -f "$DST" ]; then
mv "$SRC" "$DST"
fi
This will see if the destination exists. If not, it will move the file. Realistically there should be additional error checking, and I'm not certain a move is the best approach, since there would no longer be an "original" file.

bash script, prompt user to select ID or name completion

I am working on a script that reads the directories in a path and prompts the user for witch directory to choose. The script so far looks like this:
select choises in ${list_files[#]}; do
if CONTAINS_ELEMENT $choises "${list_files[#]}"; then
break
else
echo "invalid choise (PS use the ID numbers)! Try again:"
fi
done
the "CONTAINS_ELEMENT" thingy is a function that checks if the variable $choises is a member of the list_files array. This script works fine, and the output is something like this:
1) first_folder
2) second_folder
3) yet_another_folder
And the user can choose witch folder by typing in the corresponding ID value (ie "2" for "second_folder").
What I am struggling with is this: Is there a way to check if the supplied answer is a correct number -or- a correct file name? And if you can write in the file name manually, is there a way to implement name completion in this script?
The actual reply to select is held in the variable $REPLY, I need to somehow check both $choises and $REPLY. The completion mechanism is much more tricky I suspect..
Does anyone have a good suggestion here? It would be very cool if you could help me get in these features!!
select choises in ${list_files[#]}; do
if CONTAINS_ELEMENT $choises "${list_files[#]}" || CONTAINS_ELEMENT $REPLY "${list_files[#]}"; then
break
else
echo "invalid choise (PS use the ID numbers)! Try again:"
fi
done
This makes both the ID number and the full directory name work, the code is a bit ugly though, and there is no name completion.

What does this bash script function does

I am new to shell scripting and i found this function in a given script file.
##############################
# rotate_daily(filename)
rotate_daily() {
_code=0
_file_src=$1
_today=`date '+%Y-%m-%d'`
_file_dest=${_file_src}.${_today}
if [ -f ${_file_dest} ]; then
printk "rotate_daily(): ${_file_dest} already exist"
_code=1
else
if [ -f ${_file_src} ]; then
printk "rotate_daily(): ${_file_src} => ${_file_dest}"
cp -p ${_file_src} ${_file_dest}
_code=$?
>${_file_src}
fi
fi
}
I understand this is kind of coping file from one location to another location. But, it is not rotating right?. could somebody explain me what it is really does.
thanks in advance for any help
It copies _file_src to the location file_dest unless _file_dest already exists. An informative message will be printed that tells you if the file already exists or file_src_ will be copied, It also moves _file_src only if it is a file.
EDIT: forgot to mention what the command >{_file_src} does - it simply wipes out the contents of the source file. So you will have the contents of _file_src moved to file_dest in the end and _file_src will be empty. I can't figure why not simply do a move(with mv) and then create an empty file, but that's your question.
If the time stamped file already exists, this code snippet does nothing but print a message via printk indicating that. If it does not exist, it copies the source file to it and truncates the source file. I would guess that the line you are not quite understanding is:
>${_file_src}
That line truncates the original file after it has been copied. Note that there is a race condition, and any data written to the file between the copy and the truncation will be lost.

Resources