Execute multiple unix commands from SAS - unix

I am trying to delete a file from a SAS program. The Unix commands I want to run are:
unalias rm;
rm -f &file..txt;
How do I do this? I tried using the 'x' statement, but can't get it to work. I need the commands to run in the same shell for unalias to work AND I need the macro variable to resolve.

You can use semi-colon (;) to separate multiple commands in one command string.
x "unalias rm; rm -f &file..txt";
In general I just use a leading backslash to force Unix to NOT use the alias for command.
x "\rm -f &file..txt";

Related

what is the first step of ssh copy execute in terminal?

I am using this command to copy file from remote server to local machine:
scp -r app:/home/dolphin/model* .
In bash it works fine.In zsh it throw this error:zsh: no matches found: app:/home/dolphin/model*.I am searching from Google and understand the bash and zsh have different rule of glob.Here is my question:
what is the execute step detail of this command?
anyone could tell me the shell how to execute the command,the first step is echo the path of this command?
I could use -v(verbose) to see the scp execute process.
I am unfamiliar with Zsh, but as far as I can say, Bash will pass the original string to the program as an argument if nothing is globbed, while it appears that Zsh complains in this case.
To ensure the "unglobbed" string is passed as an argument to scp(1), you can escape the asterisk:
scp -r app:/home/dolphin/model\* .
^^

Running a script according to shebang line

I've got a script on my computer named test.py. What I've been doing so far to run the program is type python test.py into the terminal.
Is there a command on Unix operating systems that doesn't require the user to specify the program he/she uses to run the script but that will instead run the script using whichever program the shebang line is pointing to?
For example, I'm looking for a command that would let me type some_command test.txtinto the terminal, and if the first line of test.txt is #!/usr/bin/python, the script would be interpreted as a python script, but if the first line is #!/path/to/javascript/interpreter, the the script would be interpreted as javascript.
This is the default behavior of the terminal (or just executing a file in general) all you have to do is make the script executable with
chmod u+x test.txt
Then (assuming text.txt is in your current directory) every time you type
./text.txt
It will look at the sh-bang line and use the program there to run text.txt.
If you really want to duplicate built-in functionality, try this.
#!/bin/sh
x=$1
shift
p=$(sed -n 's/^#!//p;q' "$x" | grep .) && exec $p "$#"
exec "$x" "$#"
echo "$0: $x: No can do" >&2
Maybe call it start to remind you of the similarly useful Windows command.

How to correctly use the "grep" command

I am new to linux and commands.
Basically I understand "grep" command.
But I do not understand what to do with following command, what it do, how to type command correctly.
grep -R -e
Examples to use correctly are welcome.
Calling grep with those flags mean search recursively in the specified directory and all it's children for lines that match a regex.
grep -R -e /p.t/ .
Should find all lines with a p and t that has any single character in between that are in the current directory or any of it's children.
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern; useful to protect patterns beginning
with -.
-R, -r, --recursive
Read all files under each directory, recursively; this is equiv-
alent to the -d recurse option.
http://unixhelp.ed.ac.uk/CGI/man-cgi?grep

Removing files - Silent failure issue

In a Unix environment, I have a bash script that removes some files:
rm -f foo bar* baz*
My problem: not always the wildcard returns any result. And as a result of that, I fail to remove even 'foo' which always exists. The output written is "rm: No match".
A simple workaround would be to split the command:
rm -f foo
rm -f bar*
rm -f baz*
But it's a bad solution.
No it should work. Which shell is used ? Is rm an internal version or an external ? (Try /bin/rm instead to ensure the external version). You may have some shell option set that prevent you to execute the command in that case (this may depend on your shell).

Whats the difference between running a shell script as ./script.sh and sh script.sh

I have a script that looks like this
#!/bin/bash
function something() {
echo "hello world!!"
}
something | tee logfile
I have set the execute permission on this file and when I try running the file like this
$./script.sh
it runs perfectly fine, but when I run it on the command line like this
$sh script.sh
It throws up an error. Why does this happen and what are the ways in which I can fix this.
Running it as ./script.sh will make the kernel read the first line (the shebang), and then invoke bash to interpret the script. Running it as sh script.sh uses whatever shell your system defaults sh to (on Ubuntu this is Dash, which is sh-compatible, but doesn't support some of the extra features of Bash).
You can fix it by invoking it as bash script.sh, or if it's your machine you can change /bin/sh to be bash and not whatever it is currently (usually just by symlinking it - rm /bin/sh && ln -s /bin/bash /bin/sh). Or you can just use ./script.sh instead if that's already working ;)
If your shell is indeed dash and you want to modify the script to be compatible, https://wiki.ubuntu.com/DashAsBinSh has a helpful guide to the differences. In your sample it looks like you'd just have to remove the function keyword.
if your script is at your present working directory and you issue ./script.sh, the kernel will read the shebang (first line) and execute the shell interpreter that is defined. you can also call your script.sh by specifying the path of the interpreter eg
/bin/bash myscript.sh
/bin/sh myscript.sh
/bin/ksh myscript.sh etc
By the way, you can also put your shebang like this (if you don't want to specify full path)
#!/usr/bin/env sh
sh script.sh forces the script to be executed within the sh - shell.
while simply starting it from command line uses the shell-environemnt you're in.
Please post the error message for further answers.
Random though on what the error may be:
path specified in first line /bin/bash is wrong -- maybe bash is not installed?

Resources