explanations about the code - shell

I have some script and I have no idea what it is doing, will be happy if somebody will explain me:
#!/bin/tcsh
if (-d test) then
svn up test
else
svn checkout http:some address test
endif
cd tests
python test_some.py $argv
P.S can't find info about functions cd and svn
thanks in advance for any help

The script runs a second revision-controlled test script
This script runs a python program which appears to run some tests. The script understands that the test directory is stored in a subversion repository.
If there is a test directory, it updates it in case it has been changed in the repository, perhaps by another svn user or by the same user in a different working directory.
If there is no test directory, it checks it out.
Then it changes its current directory to the working directory.
Then it runs the test script.
I'm a bit confused about one thing. It checks out "test" but then changes its directory to "tests". So either there is a transcription error in the original post or something slightly more complex is going on, like, it somehow assumes that tests exists but not test.

cd is the "Change Directory" command.
svn is a source code repository client.

The script does the following:
if the test folder exists
update it through subversion
else
check it out from subversion repository
go into the tests directory // interestingly enough, it doesn't match the checked out directory name?
run the test_some.py python file, passing the script arguments.

cd, and svn, and python are executable names. cd is the command for changing current directory. svn is the command (executable name) for Subversion source control system. python is the Python language interpreter.

Related

Sourcing the source files using bash script

Usually I source all the macros I have for the jobs run in a remote machine using this command:
macros=$\my_directory
But I see someone uses a different way to get all the macros for submitting the jobs in a remote machine. He uses this command:
macros=$(dirname $(readlink -f $BASH_SOURCE))
Now I want to know how the $dirname has the advantages over giving the specific macro location. It would be great if you just explain to me regarding the sourcing the macro using $dirname
By using dirname you get the directory of where the script is located, therefore it's easy to source other files locally close to your script and don't worry about specifying the correct path each time the script bundle is relocated.
For instance if you have in your script source $macros/some_script.sh then it will not break when the bundle is located in the /usr/local/bin/ or /bin/ or ...
Regarding $BASH_SOURCE see: https://stackoverflow.com/a/35006505/2146346

Run script on remote server from local machine

I have a remote script on a machine (B) which works perfectly when I run it from machine (B). I wanted to run the script via ssh from machine (A) using:
ssh usersm#${RHOST} './product/2018/requests/inbound/delDup.sh'
However, machine (A) complains about the contents of the remote script (2018req*.txt is a variable defined at the beginning of the script):
ls: cannot access 2018req*.txt: No such file or directory
From the information provided, it's hard to do more than guess. So here's a guess: when you run the script directly on machine B, do you run it from your home directory with ./product/2018/requests/inbound/delDup.sh, or do you cd into the product/2018/requests/inbound directory and run it with ./delDup.sh? If so, using 2018req*.txt will look in different places; basically, it looks in the directory that you were in when you ran the script. If you cded to the inbound directory locally, it'll look there, but running it remotely doesn't change to that directory, so 2018req*.txt will look for files in the home directory.
If that's the problem, I'd rewrite the script to cd to the appropriate directory, either by hard-coding the absolute path directly in the script, or by detecting what directory the script's in (see "https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within" and BashFAQ #28: "How do I determine the location of my script? I want to read some config files from the same place").
BTW, anytime you use cd in a script, you should test the exit status of the cd command to make sure it succeeded, because if it didn't the rest of the script will execute in the wrong place and may do unexpected and unpleasant things. You can use || to run an error handler if it fails, like this:
cd somedir || {
echo "Cannot cd to somedir" >&2
exit 1
}
If that's not the problem, please supply more info about the script and the situation it's running in (i.e. location of files). The best thing to do would be to create a Minimal, Complete, and Verifiable example that shows the problem. Basically, make a copy of the script, remove everything that isn't relevant to the problem, make sure it still exhibits the problem (otherwise you removed something that was relevant), and add that (and file locations) to the question.
First of all when you use SSH, instead of directly sending the output (stdout and stderr) to the monitor, the remote machine/ssh server sends the data back to the machine from which you started the ssh connection. The ssh client running in your local machine will just display it (except if you redirect it of course).
Now, from the information you have provided, it looks like the files are not present on server (B) or not accessible (last but not least, are you sure your ls target the proper directory? ) you could display the current directory in your script before running the ls command for debugging purpose.

Git Bash file which runs command in current directory, then opens chrome

Per the title, I'd like to have a .sh file which I can drop into a directory and then:
Run http-server (the simple node server) in the current directory
Open Chrome and point it to that server
The idea is that when I'm developing I can quickly run this bash file and see the current version of whatever html/css/javascript website I am working on. My issue is that if you run a command in a bash file, it isn't run in the current working directory, but rather in the root directory (as far as I can tell). So if I just write http-server in my file, it will run a server not in the current directory, but in ./.
To fix this, I want to cd to the directory first, and then run the script. current_dir=$(pwd) will give me something close to the current directory, but I can't put that directly into a cd command because it (1) doesn't have quotes around it, so spaces in directory names will make it not work, and (2) it starts with /C/ instead of /C:/. Can anyone advise me on how to fix this?
My current code looks as follows.
curr_dir=$(pwd)
cd $curr_dir
http-server
start chrome localhost/XXX
And, as mentioned, results in the http-server command being run in the wrong place:
Starting up http-server, serving ./
Available on:
http://192.168.56.1:8081
http://192.168.1.21:8081
http://127.0.0.1:8081
Hit CTRL-C to stop the server
e: For anyone who happens on this later, this isn't actually a problem -- see the accepted answer. I had another typo.
No; the commands you run always run in the current directory. Otherwise, if your hypothesis were correct, e.g. ls would always show the files in the root directory.
If you want to run a command which is in a different directory, you want
../relative/path/to/command
or
/absolute/path/to/command
Neither of these change the current directory of the shell. The current working directory of the process you create will remain the directory you were in when you run this command. (Even if you run it in the background and subsequently change to a different directory in your interactive shell, for example.)
If you want the command to run in the directory where the script lives on the disk, something like this can occasionally be useful.
cd "$(dirname "$0")"
but again, most of the time, you want and need your commands to run in the current directory.
(There are situations where you want a script to process data files in a fixed location, but these are rare exceptions. Until you have such a situation, consider it a bug to use cd in a shell script.)
If you have a command http-server somewhere on your PATH, just http-server will run that. You should normally not have the current directory on the PATH, but to run the binary in the current directory instead of from anywhere on the PATH, you say so:
./http-server

Create Working Folders using stcmd

I have been trying to write an stcmd that checks out code from a StarTeam repository. Here's what the command looks like:
stcmd co -p "Username:Password#localHost:1024/Store Server/Store Server/USB/sources/$OEM$/$$/Setup/Scripts"
Every time I run this code, I get the following response:
C:\StarTeam\Store Server\USB\sources\$OEM$\$$\Setup\Scripts\osConfig.ps1 (The system cannot find the path specified)
I'm guessing I need to have to create the working folder's location in order for my check-out command to work properly. Is there a way to create the working folders of a repository using stcmd? I know I can do it through StarTeam, but I wanted to see if it's possible to create it through stcmd so it can create the folders on new computers when my code runs.
You haven't said which version you're using, but in 5.4 the command to create working directories is:
stdcmd local-mkdir
so you'd need something like:
stdcmd local-mkdir -p "Username:Password#localHost:1024/Store Server/Store Server/USB/sources/$OEM$/$$/Setup/Scripts"
This is the answer to your question, but I'm not sure it'll be the solution to your problem!
Not sure which version of StarTeam you're using, but in 13.0 at least, there's an option -cwf (checkout working folders) which you can append to the check-out command. if you also want this to checkout subfolders of said working folders, you can also append -is (include subfolders, maybe?). So, try:
stcmd co -p "Username:password#localHost:1024/Store Server/Store Server/USB/sources/$OEM$/$$/Setup/Scripts/" -cwf -is

Cd in shell script not working

First off I am very very new to shell scripting. I am trying to write a script that takes in one parameter and then copies a folder in a different directory naming it using the parameter. This is the current code that I have:
#!/bin/sh
cd /var/www/html/fbplugin/chrome
sudo mkdir temp/$1
sudo cp -rf "/var/www/html/fbplugin/chrome/fbplugin" "/var/www/html/fbplugin/chrome/temp/$1"
When I run this code it says can't cd to /var/www/html/fbplugin/chrome. I'm not sure why it is saying this because I know the directory exists. I have copied the line directly and it works in terminal. If anyone could help me out that would be great.
If it matters in order to run the script I am typing "sh build.sh"
If that directory really exists, then you must have executed that script with a different user (cron, webserver, etc).
Check the rights for that directory.
I don't know why you're getting the error about cd, but it looks like you could just use absolute paths throughout. That would solve the larger problem of the script working correctly.

Resources