How to use "tee" with "source" command in Linux? - linux

On Linux I'm using "tee" to capture the output of "source" command and print it to output log file, but failed. The command I'm using is like this:
source ./my_run.sh 2>&1 | tee -i my_run_log
The intention of my_run.sh is to "make" some compile job, as well as some routine jobs like cd, rm and svn update. The content of my_run.sh is like follows:
make clean
cd ..
rm ./xxx
svn up -r 166
cd ./aaa/
sed -i -e ......
make compile
make run
However, when I run it the "tee" just does NOT work, and do NOT give me the log file at all. In order to verify that the entire environment is good, I did a simpler test with:
ll 2>&1 | tee -i log
and in this simpler scenario the "tee" works perfectly fine and prints out "log" as I expected.
Can anyone help me find out where my problem is?
btw,
I'm working on Red Hat Linux (Release 5.9), using bash shell.
Thanks in advance!
SOME MORE COMMENTS:
I did some more tests and found that as long as the my_run.sh script has got "make xxx" stuffs in it, then "tee" will fail. Seems like tee does NOT like make. Any solutions?

Problem solved; many thanks goes to #thatotherguy in leading me to the solution. The log output was actually deleted by the make clean process. After fixing the clean stuff in the makefile, everything is good.

Related

How to run an AppImage with flags using a bash script?

I'm trying to create a bash script to run an AppImage on startup with certain flags.
This is the normal way to run through terminal on the folder where the appimage is located:
$ ./conky.AppImage -c $HOME/Downloads/Pleione/Pleione.conf &> /dev/null &
And I'm trying to put that in a bash script. I have this:
#!/bin/bash
cmd= $(echo /home/edbanshee/Downloads/conky.AppImage)
$cmd
And of course, it runs the AppImage, however I have no idea how to include the -c flag with the parameters. To my very limited knowledge, I tried something like:
#!/bin/bash
cmd= $(echo /home/edbanshee/Downloads/conky.AppImage)
$cmd -c $HOME/Downloads/Pleione/Pleione.conf &> /dev/null &
To no avail so I guess thats not how that works (sorry if these are silly attempts, I am learning by trial and error)
Yet i can't seem to find any information about bash scripting on how to accomplish this, or maybe I'm bad at looking. I found tutorials regarding getops, but as I understand it, that works to pass flags onto the script itself, which doesn't seem to be what I'm looking for, or am I understanding it incorrectly?
Any suggestions?

sh script with scp variables

I am trying to writing a simple script which will figure out the latest version of a file by the filename and then download that file to the local computer.
What I cant figure out is why my code will work in the shell, but not work when I run it as a script. I am also running my script on cygwin, not sure if that will make a difference.
Here is the script
#!/bin/sh
x=$(ssh user#hostname 'ls -r -t /vgf/day1*.gif | tail -1')
echo $x
scp user#hostname:"${x}" /images/day1.gif
x is correctly assigned, but when I get to the scp command I receive something along the lines of
: No such file or directoryif
However if I run the scp command in the shell it will work
$ sh download.sh
/vgf/day1.gif
: No such file or directoryif
$ scp user#hostname:"${x}" /images/day1.gif
day1.gif 100% 22KB 22.1KB/s 00:00
I would be open to different solutions. If I could prevent the version increasing via some linux administration, I may follow that route, although I am still wondering what the problem is here.
By version, I mean day1_001.gif and the new version becoming day1_002.gif and so on. So when the file saves a file day1.gif, it will overwrite the original without creating another version.

Linux startup init.d file always fails with following bash, whats wrong?

I have the following bash script to update to my website my current ip. It works fine stand alone, but put into a startup file, fails upon startup. I'm guessing it's a sequencing thing, but I'm not sure how to fix the sequencing, and after a few hours of googling and trying everything I can think of, I'm hoping someone can lead me in the right direction! This is what I am trying to run:
#!/bin/sh
IP_ADDR=$(ifconfig eth0 | sed -rn 's/^.*inet addr:(([0-9]+\.){3}[0-9]+).*$/\1/p')
wget -q -O /dev/null http://example.com/private/RPi_IP.php?send=${IP_ADDR}
I can't figure out what to do. I tried adding it to other startup programs even, and it fails upon startup too. I'm using a Raspberry Pi. Any ideas?
Your path might not be what you expect. You should fully-qualify any commands that you use. Especially for programs that live in /sbin/
ie
/sbin/ifconfig
/usr/bin/sed
/usr/bin/wget

cp command won't run if executed from shell script

i have very simple shell script
#!/bin/bash
cp -rf /var/www/ksite/app2/* /var/www/ksite/app
echo "----"
echo "done"
but seems cp command fails
if i execute
cp -rf /var/www/ksite/app2/* /var/www/ksite/app
from terminal everything work ok. Can someone tell me how to include cp in shell script?
Thanks
We seem to have doubt as to how this script fails. If there is no error message then this is a strange one. I suggest:
On the command line (which works), do a which cp
Whatever the reply, then copy that and use it as the cp in the script (e.g. /bin/cp)
Check the widcard expansion, run your script with bash -x script-name and see if you get what you expect.
echo $? after the copy in the script - if it is zero then it (thinks it) worked.
Do a ls -ld /var/www/ksite/app from your script, maybe someone set a symbolic link?
If it still fails, source the script from the command-line and see if that works . script-name
Double check that the copy did actually fail! (maybe that should be step 1.)
Make sure you really have bash at /bin/bash. I think a batter hash bang is:
#!/usr/bin/env bash
This uses the env command to locate the bash binary and set the environment.
I had similar problem. What helped me:
I used windows and putty to write script, so I had \r\n at the end of lines. Be sure, you have only \n symbol.
I copied files and the only way it worked for me at script was cp <source_dir>/fileName <dest_dir>/fileName whereas at command line cp <source_dir>/fileName <dest_dir> worked well too.
Just covering all the bases .. do the permissions vary between the excutions .. i.e. do you execute one with sudo/root privileges, the other as user (unlikely, but thought I'd ask since we don't know what the exact error is)
Similar issue to Vladmir where the script was created in Windows. I created a new file "my_bash_script.sh" in the linux environment using VIM, then read the contents of my script into the file:
:r file_made_in_windows.sh
Then I saved, closed, then set the file as executable:
chmod 744 my_bash_script.sh
From there, I ran the script:
./my_bash_script.sh
...and it worked. What a weird issue. I was confounded for a moment.

shell script in unix(in bash shell) for copying a file from one location(i.e a directory), to other, without using cp command at all

Can anyone help me with a shell script in unix(in bash shell) for copying a file from one location(i.e a directory), to other, without using cp command at all(not using it in the script also.Any other command /utility can be used in the script).Can it be really done ? I could'nt find a way out.Any help would be appreciated...
Presumably you can cat and redirect the output. Since this sounds like homework, I'll leave it to you to work out the exact syntax.
How many options do you want?
rcp
scp
dd
perl
python
cat
while / read / echo
That is nowhere near exhaustive. You can be creative...have fun!
You could use:
cat to create a C source file, copy.c
make or cc to compile it
And then run it to do the copying.
You could use:
cat /usr/bin/cp > x
chmod +x x
./x old new
But that is probably cheating.
Erghm. Why?! Anyway...
cat file1 > some/other/dir/file2
install is a good one.

Resources