Using "find" in csh script file - unix

I run a .csh file in UNIX that contains the following script
#!/bin/tcsh -f
set path = "$1"
find "$path" -name myfolder
And get the following message
find: Command not found.
What am I missing?
Thanks

The $path variable is special - it tells the shell where to find tools like find. :-) Use a different variable name.
From your interactive shell, you can see what $path normally looks like by echoing it. The following is my path on my FreeBSD server:
ghoti% echo $path
/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /home/ghoti/bin /usr/X11R6/bin /usr/games
If this list is replaced with something else, for example the contents of $1, then tcsh doesn't know to look in /usr/bin to find find:
ghoti% which find
/usr/bin/find
ghoti% set path = "hello world"
ghoti% which find
find: Command not found.
ghoti%

Related

grep/sed not found error, not in $path

I'm a unix newbie here and I have a unix command I'm trying to run but I get a "GREP: not found" error. I looked at $PATH and didn't see anything resembling grep (not sure if thats what i'm looking for either though)...
The command is this:
testabcd=$(bteq << EOF 2>&1 |grep '^>' |sed -e "s/^>//"
.LOGON server/user, pass
DATABASE schema;
.set width 2000;
.set titledashes off;
SELECT '>'||COUNT(*) FROM schema1.table1;
.LOGOFF;
.QUIT;
.EXIT
EOF)
echo "The count is: " $testabcd
then I get these errors:
-ksh: SED: not found (No such file or directory)
>echo "The count is: " $testvarabcd
THE DATA IS:
>-ksh: GREP: not found
*** Error: The following error was encountered on the output file.
*** Error: Broke pipe
*** Warning: Canceling the rest of the output
if grep is not in PATH, do I need to install it? If not, can I set the path in the command and how do I search where the grep path is??
replace grep with /bin/grep and sed with /bin/sed.
ultimately you need to add /bin to your path.
do you like ksh? In my experience, csh, tcsh, or bash are
more commonly used. Using one of those may give you a better path.
Then you would not need to edit your path.
the file that contains you path is a hidden file (it has a . in front
of the file name) in your home directory. try ls .* in your home directory.
Even better, try
/bin/grep PATH .*
this will locate the file with the PATH variable.

Where is my $PATH set?

I'm trying to add a directory to my $PATH in mac OSX.
echo $PATH produces:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin
logged in as user and doing: vim ~/.profile shows a different set of paths and adding to it doesn't change the path echo'ed from: echo $PATH (in a new shell)
I have looked in ~/.bash_profile and its not there.
I've tried looking around when logged in as sudo su - but I can't find where the above path is set for me to edit it.
echo $env shows a blank line.
You should add the path to .bashrc and also to source it from your .bash_profile like so
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
That way, when you set a PATH, it will apply to both.
Now when you login to your machine from a console .bashrc will be called.

How to execute bash script from any location?

In UNIX, I read that moving a shell script to /usr/local/bin will allow you to execute the script from any location by simply typing "[scriptname].sh" and pressing enter.
I have moved a script with both normal user and root permissions but I can't run it.
The script:
#! bin/bash
echo "The current date and time is:"
date
echo "The total system uptime is"
uptime
echo "The users currently logged in are:"
who
echo "The current user is:"
who -m
exit 0
This is what happens when I try to move and then run the script:
[myusername#VDDK13C-6DDE885 ~]$ sudo mv sysinfo.sh /usr/local/bin
[myusername#VDDK13C-6DDE885 ~]$ sysinfo.sh
bash: sysinfo.sh: command not found
If you want to run the script from everywhere you need to add it to your PATH. Usually /usr/local/bin is in the path of every user so this way it should work.
So check if in your system /usr/local/bin is in your PATH doing, on your terminal:
echo $PATH
You should see a lot of paths listed (like /bin, /sbin etc...). If its not listed you can add it. A even better solution is to keep all your scripts inside a directory, for example in your home and add it to your path.
To add a directory in your path you can modify your shell init scripts and add the new directories, for example if you're usin the BASH shell you can edi your .bashrc and add the line:
PATH=$PATH:/the_directory_you_want_to_add/:/another_directory/
This will append the new directories to your existing PATH.
You have to move it somewhere in your path. Try this:
echo $PATH
I bet /usr/local/bin is not listed.
I handle this by making a bin directory in my $HOME (i.e. mkdir ~/bin) and adding this to my ~/.bashrc file (make the file if you don't already have one):
export PATH=~/bin:$PATH
This may seem silly to mention, but did you make sure it is executable? Did you chmod +x script.sh? Does the shell script have the correct path to it's shell at the top (i.e #!/bin/bash)? Also, are you using UNIX or LINUX or FreeBSD? (last question is important)
To run executable from any directory:
1)Make a bin directory under your home directory and mv your executable scripts into it.
[root#ip9-114-192-179 ~]# cd /home
[root#ip9-114-192-179 home]# mkdir bin
[root#ip9-114-192-179 home]#ls
bin cloud-init-0.7.4-10.el7.noarch.rpm cloud-user epel-release-7-11.noarch.rpm
2)Move your executable scripts in bin direcoty.
mv preeti.sh /home/bin
3)Now add it to your path variable.And source it.
[root#ip9-114-192-179 ~]# echo 'export PATH="$PATH:/home/bin"' >> /etc/profile
[root#ip9-114-192-179 ~]# source /etc/profile
[root#ip9-114-192-179 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/home/bin
4)Check if that path is added in path variable.
[root#ip9-114-192-179 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/home/bin
5)Verify if script is running from any random directory.

Using grep to find a file that contains a string

My .htaccess file in my htdocs folder does not work. I tried to redirect to Google when accessing a filename. I want to find out where the settings for my httpd.conf are, so I can enable mod_rewrite. I did the following UNIX command to find out if a httpd.conf file existed on my hard drive:
find * -name "httpd.conf"
The file does not exist. I am thinking that maybe there is another file that controls mod_rewrite. I want to see if "AllowOverride" exists in any directory. I entered the following UNIX command:
grep -r "AllowOverride" *
But it's hard to read because it prints out so many folders. The message that accompanies the folders are "Permission denied" or "No such file or directory". How do I only get the file paths of files that contain AllowOverride?
Many Unix and similar systems provide a locate(1) command that uses a database to speed finding individual files. Try this:
locate httpd.conf
Note, of course, that Apache configurations are stored in files of all sorts of names; I've seen apache.conf, httpd.conf, httpd2.conf, and then there's the giant pile of /etc/apache2/conf.d/ -- entire directory structures set aside for configuring Apache. Your distribution may vary.
Perhaps apachectl configtest will show the paths? (currently not installed on my machine, so I can't easily test.)
Try this command:
find / -name "httpd.conf" 2>1 | grep -v "Permission denied"
the 2>1 funnels stderr to stdout so that both can be piped into the grep utility. grep in turn will print anyline that doesn't have the string "Permission denied" in it (the -v negates/inverts the matching of the search string)
If you don't redirect stderr to stdout, the output of stderr to the console would bypass the rest of the command line.
You could extend the above command line by appending this:
| grep -v "No such file or directory"
if that string was coming up and you wanted to suppress it too.
This tells you all about io redirection. And here's a nice quick summary.
Use the following:
find / -type f -exec grep -n "AllowOverride" {} \; -print 2>/dev/null
To scan files containing the "AllowOverride" string from the root, if you want to run the search in a particular directory, use the following instead:
find /path/to/directory -type f -exec grep -n "AllowOverride" {} \; -print 2>/dev/null
The output should only print the files containing the specified string along with the number of the matching line

Unix - how to source multiple shell scripts in a directory?

when I want to execute some shell script in Unix (and let's say that I am in the directory where the script is), I just type:
./someShellScript.sh
and when I want to "source" it (e.g. run it in the current shell, NOT in a new shell), I just type the same command just with the "." (or with the "source" command equivalent) before it:
. ./someShellScript.sh
And now the tricky part. When I want to execute "multiple" shell scripts (let's say all the files with .sh suffix) in the current directory, I type:
find . -type f -name *.sh -exec {} \;
but "what command should I use to "SOURCE" multiple shell scripts in a directory"?
I tried this so far but it DIDN'T work:
find . -type f -name *.sh -exec . {} \;
and it only threw this error:
find: `.': Permission denied
Thanks.
for file in *.sh
do . $file
done
Try the following version of Jonathan's code:
export IFSbak = $IFS;export IFS="\0"
for file in `find . -type f -name '*.sh' -print0`
do source "$file"
end
export IFS=$IFSbak
The problem lies in the way shell's work, and that '.' itself is not a command (neither is source in this). When you run find, the shell will fork itself and then turn into the find process, meaning that any environment variables or other environmental stuff goes into the find process (or, more likely, find forks itself for new processes for each exec).
Also, note that your command (and Jonathan's) will not work if there are spaces in the file names.
You can use find and xargs to do this:
find . -type f -name "*.sh" | xargs -I sh {}

Resources