Here is my assignment for class: I know you don't post specific questions, but here's what I have tried and it isn't working. I was hoping for someone to point me in the direction and I can go from there:
Write a shell script that performs the following functions:
as the first command in your shell script use the script -a ch10-q1.txt command.
use the echo command to display the hostname, logname, and home system variables.
displays the current date and time using Coordinated Universal Time.
displays the list of directories in the user's home directory and all the subdirectories below (hint: use the tree command with the appropriate option).
list all the files in the user's home directory and all the subdirectories below (hint: check the options). Also use the -gF options.
use the df command to display the space usage in your system. Use the option(s) to include the total size in human readable format.
Save your shell script file in your home directory and name it ch10-1
Here is my code in vi ch10-1.
# !/bin/bash
script -a ch10-q1.txt
echo $hostname
echo $date -u
echo $ls -d */u
echo $ls -la
echo $ls gf
echo $df; df -h
Then I save the file and make the file executable by:
chmod 777 ch10-1
I try and run the program by:
./ch10-1
And then it tells me that line 2-8 command not found.
I guess my questions is how do I have multiple commands?
$hostname
The command to display the hostname.
Related
Why can't I run a bash script in the current directory I'm in?
Whenever I run the script the commands are executed in the home directory.
The only answers I found are included below.
I do use the zsh shell. I don't know if that changes anything.
Thanks in advance!
What I have tried so far:
#!/bin/bash
touch test.txt
#!/bin/bash
cd $PWD
touch test.txt
#!/bin/bash
variable = $PWD
cd $variable
touch test.txt
#!/bin/bash
variable= pwd
cd $variable
touch test.txt
#!/bin/bash
cd -
touch test.txt
If I run the script for example from /home/user/dir1/dir1.1 the test.txt file is created in the home directory (/home/user) and I get redirected to the home directory as well.
in bash there are two things to do:
ensure that the shell script file is saved properly and is chmod'd to be an executable.
To do so, save the file (e.g. script.sh) with the code you want, and then run chmod +x script.sh to make linux understand that this file is an executable.
call the executable properly using the ./script.sh command. alternatively, you can also call the script from remote folder by calling it using the absolute path the script is in (e.g. /folder/folder/folder/script.sh).
This should execute the file. from there, it's about your code and if you need help there, please update your question.
I'm trying to add a context menu option to Nautilus so that when I right click on a file, I can choose to run srm to securely overwrite it with zeros instead of moving it to /Trash or rm'ing it. I have the following executable in ~/.local/share/nautilus/scripts
#!/bin/bash
#Make local Nautilus filepath variable global
export srmthis=$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
#Copy the above variable to a log
echo "$srmthis" >> logfile.txt
#Now, please srm secure-delete the file indicated in the filepath
sudo -E /usr/bin/srm -flz "$srmthis"
$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS is a variable set by Nautilus when a file is selected within it. My thinking is that I'd grab that into my own variable $srmthis and then pass it to srm as its target file with options "-flz" for a faster, albeit more insecure wipe.
But it doesn't work. No output, no popup warning.
When I echo "$filetosrm" >> somefile.txt I do get an output so I know the variable is set. I can also use srm by itself in the terminal no problem.
What am I doing wrong?
Many thanks!
P.S. Tried running srm with gksu, pkexec, | xargs, etc to no avail.
Figured this out. Here's the new code that works:
#!/bin/bash
#Make local Nautilus filepath variable global
srmthis=$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
#Copy the above variable to a log
echo "$srmthis" >> srmtemp.txt
#Get the first line from temp file to get rid of annoying newline
line=$(head -n1 srmtemp.txt)
#Wipe the file with srm: -fllz fast mode, -r recursively for any subdirectories
/usr/bin/srm -fllzr "$line"
#Clean up
/usr/bin/srm -l srmtemp.txt
Also, instead of using sudo /usr/bin/srm I ended up adding srm to my /etc/sudoers for my username with whiterabbit ALL = (root) NOPASSWD: /usr/bin/srm which allowed it to run without having to have the command sudo in the script.
This adds srm as a Nautilus script. I can now rightclick on a file and then go to Scripts -> SRM and wipe-delete the file with zeros right in the GUI instead of rm'ing it or moving it to trash.
I have a .sh file and I want to execute it from shell without writing the extension.
What I did:
I created a directory and added it to $PATH
I gave to the file.sh chmod 711
and the file contain #!/bin/sh (I tried also bash).
However when I try to execute myscript without sh I get command not found
while if I try with myscript.sh I get the right result.
How could I do?
I read also: How to run a shell script on a Unix console or Mac terminal? and executing shell script without calling sh implicitly but no solution
Result of ls -l
ls -l /Users/Mitro/scripts
total 8
-rwx--x--x 1 Mitro staff 22 Nov 26 10:25 myscript.sh
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/Mitro/scripts
Two problems...
Firstly, file is already an executable program in /usr/bin that tells you the type of a file - i.e. whether it is an image, or a song or a database. Quick example:
file a.png
a.png: PNG image data, 1 x 1, 1-bit colormap, non-interlaced
So, file is a bad name for a shell script - likewise is test.
Secondly, if you want to execute a script or program in your current directory, also known as dot (.), you either need to have dot in your PATH, or you need to explicitly tell your shell that the file you want to run is in the current directory. The easier option is the second, which means if your script is called fred, you run it with
./fred
which tells the shell it is in your current directory.
The longer option, if you want to always be able to run scripts in the current directory, is to add dot to your PATH. So, you locate your login script (probably $HOME/.profile) and you find the line that sets your PATH and you add the current directory to it.
export PATH=$PATH:.
Once you have set that, you are best off logging out and back in to have it take effect.
Some folks disapprove of the idea of adding dot to their PATH - I don't. YMMV.
You can add alias. If you have /some/path/to/script.py, do:
alias my_script='/some/path/to/my_script.py'
Now when you enter my_script, your script would be executed.
For mac the profile file is ~/.bash_profile as opposed to ~/.profile which did not work.
Im trying to write a script that rsh's over to a unix machine and executes certain commands on certain files
rsh's over to machine (in this case a machine called uk01ff200)
searches for a directory in machine
then within that directory searches for a files starting with core
executes a command on those files (if they exist) which then creates new files
SCRIPT SO FAR:
#!/bin/bash
#
MACHINE=uk01ff200
DIRECTORY=/var/core
rsh $MACHINE "cd /var/core"
for file in `ls -1 core.*`
do stack_dump $file
done
When I do this manually in the shell on the command line it works. So if I rsh over to the machine, cd to the directory, then type in the for loop it works (so I know the for loop syntax is correct). So I dont know where I'm going wrong with my script.
What I would do using a here-document :
#!/bin/bash
#
MACHINE=uk01ff200
DIRECTORY=/var/core
rsh $MACHINE bash <<'EOF'
cd /var/core
for file in core.*
do stack_dump "$file"
done
EOF
I wish to write a simple csh script which loops through all computers in a network and executes a command that is input at the command line
echo -n "Please enter command you would like executed on all computers > \n "
set command = "$<"
say the user enters ls | grep something. How would I then execute this command in the following line? I tried
$command which works fine for input such as echo "Hello World". I get the following error for ls | grep something
ls: |: No such file or directory
ls: grep: No such file or directory
ls: something: No such file or directory
Ideally, I would want to enter several commands at the command line before looping through each computer in the network (which I can already do) and execute. Eg say I wish to copy two different files
sudo cp ./bin/elastix /usr/bin; sudo cp ./lib/transformix /usr/lib
Thanks
loops through all computers in a network and executes a command that is input at the command line
You would be perhaps using ssh so you would do something like:
ssh $hostname "$command"
I'm not a csh user so I may have the syntax wrong. For current machine you may use the eval shell command that should interpret any command sequences, not only simple commands.