This could be a dumb question, but how do I make it so a script can be called with "sh [Script Name] [File Path]" and have the script automatically read the file path?
For instance, if I use cp ~/usr/local/bin/testScript ~/Desktop it knows that the target file is the testScript file because it read the path from the command line.
I have a script based on ImageMagick that prompts the user for the file path using "read", processes it, and then drops it onto the desktop. I want the script to skip the actual prompt for the file path, and instead just target whatever file path is entered after calling the script (I.E. sh ConvertPDF /Users/ProfileName/Desktop/testFile.tiff
Positional parameters to the script are available via $n where n >= 1. For n > 9 braces are required in order to distinguish from n < 10.
#!/bin/sh
echo "$2" "$3" "$1"
...
$ ./somescript foo bar 42
Related
I compiled mplayer from source on Ubuntu. I didn't want to use a GUI but I wanted to make a executable bash file that gets the path from an file that gets dropped onto the bash file. How do I make such a thing possible?
I wanted to make it look something like this:
mplayer <get full path to file.file-ending>
I want the executable bash file to sit on my desktop ;)
If possible, I'd just like an rightclick -> start with mplayer function, but I don't know how to make one.
You can access arguments passed to the script with $1 (for the first argument). And also you should make a .desktop file so Nautilus (or your desktop manager) know what to do and use %u to pass the dropped path to the script.
For example you can create a file named DropOverMe.desktop:
[Desktop Entry]
Encoding=UTF-8
Name=Drop Over Me
Comment=Execute the script with the file dropped
Exec=gnome-terminal -e "/folder/to/the/script/launchme.sh \"%u\""
Icon=utilities-terminal
Type=Application
I use gnome-terminal as I have Ubuntu on my PC, use your preferred terminal application.
And the script could be something like:
#! /bin/bash
echo "Launched with $1" >> /tmp/history.log
Try:
#!/bin/bash
mplayer "$1"
The file path of the dropped file will be passed to the script file as the 1th command line argument.
In openned terminal
By using mate-terminal, gnome-terminal or konsole, you could use drag'n drop into oppened window.
This will send URL as tipped, with a space added, but without endline.
For this, run mplayer, I wrote this little loop function:
while IFS=$' \t\r\n' read -d '' -p "Wait for URL to play: " -rsn 1 str &&
[ "$str" ];do
while IFS= read -d '' -rsn 1 -t .02 char
do str+="$char"
done
if [ "$str" ] ;then
read -a req <<<"$str"
echo $req
mplayer $req
fi
done
First read will determine if something is comming or else end loop.
Second loop with very short timeout will read dropped URL as a single string
read -a req will split string to consider only 1st part
I have a bash script like this where I pass in a file path into it
groups=$1
file=$2
if $groups
then
sh with_groups.sh $file
else
sh without_groups.sh $file
fi
What I currently do: Open the folder where my file is, do pwd, copy the link, add some_csv.csv to it and run the above script. I want to find a more efficient way to do it.
The equivalent of what I would would be something like
sh ./../run_script.sh true pwd/some_csv.csv
where pwd is the current path I am in (true refers to the first argument. You can disregard that for the purpose of this question)
There is a similar question about this issue. But not the same solution.
I am to create a shell script that takes two parameters:
1.the desired file extension
2.the name of a single file to be renamed with that extension
The script should rename the file with the desired file extension. If the file does not exist, it should print out "fileName: No such file". It is producing this message but the professor's tests says it is producing unexpected messages(extra messages) but it is not. My shell script is:
#!/bin/sh
fileExtension="$1"
shift
oldName="${#}"
extension=${oldName##*.}
if test -r "$oldName"
then
if "$fileExtension" == $oldName.*
then
echo "$oldName"
else
newName="${oldName%.*}.$fileExtension"
mv "$oldName" "$newName"
fi
else
echo "$oldName": No such file
fi
Everytime I test it, it produces "fileName: no such file" and nothing else.
The test is executed by
./chExt2.sh cpp aardvark.CPP
where aardvark.CPP is not on the directory.
Any help or guidance would be much appreciated. Thank you
Your shebang is telling your shell to use /bin/sh to run the script. /bin/sh is typically a symlink to the real/default shell on the host. For example, mine's bash:
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 27 2009 /bin/sh -> bash
It sounds like your professor's computer's /bin/sh is using a different shell than you are [expecting]. This script runs fine in ksh or bash, for example, but produces "extra output" if /bin/sh is tcsh:
fileExtension=cpp: Command not found.
Illegal variable name.
how I can use the second argument of previous command in a new command ?
example, with
$ mkdir test
I make a directory, how I can use the name of directory for change to this ?
$ mkdir test && cd use_var
$_ is the last (right-most) argument of the previous command.
mkdir gash && cd "$_"
(I don't create files or directories called test, that's the name of a shell built-in and can cause confusions)
With history expansion, you can refer to arbitrary words in the current command line
mkdir dir1 && cd "!#:1"
# 0 1 2 3 4
!# refers to the line typed so far, and :1 refers to word number one (with mkdir starting at 0).
If you use this in a script (i.e., a non-interactive shell), you need to turn history expansion on with set -H and set -o history.
Pressing Esc + . places the last argument of previous command on the current place of cursor. Tested in bash shell and ksh shell.
I use functions for this. Type this in your shell:
mkcd() { mkdir "$1" ; cd "$1" ; }
Now you have a new command mkcd.
If you need this repeatedly, put the line into the file ~/.bash_aliases (if you use bash; other shells use different names).
I compiled mplayer from source on Ubuntu. I didn't want to use a GUI but I wanted to make a executable bash file that gets the path from an file that gets dropped onto the bash file. How do I make such a thing possible?
I wanted to make it look something like this:
mplayer <get full path to file.file-ending>
I want the executable bash file to sit on my desktop ;)
If possible, I'd just like an rightclick -> start with mplayer function, but I don't know how to make one.
You can access arguments passed to the script with $1 (for the first argument). And also you should make a .desktop file so Nautilus (or your desktop manager) know what to do and use %u to pass the dropped path to the script.
For example you can create a file named DropOverMe.desktop:
[Desktop Entry]
Encoding=UTF-8
Name=Drop Over Me
Comment=Execute the script with the file dropped
Exec=gnome-terminal -e "/folder/to/the/script/launchme.sh \"%u\""
Icon=utilities-terminal
Type=Application
I use gnome-terminal as I have Ubuntu on my PC, use your preferred terminal application.
And the script could be something like:
#! /bin/bash
echo "Launched with $1" >> /tmp/history.log
Try:
#!/bin/bash
mplayer "$1"
The file path of the dropped file will be passed to the script file as the 1th command line argument.
In openned terminal
By using mate-terminal, gnome-terminal or konsole, you could use drag'n drop into oppened window.
This will send URL as tipped, with a space added, but without endline.
For this, run mplayer, I wrote this little loop function:
while IFS=$' \t\r\n' read -d '' -p "Wait for URL to play: " -rsn 1 str &&
[ "$str" ];do
while IFS= read -d '' -rsn 1 -t .02 char
do str+="$char"
done
if [ "$str" ] ;then
read -a req <<<"$str"
echo $req
mplayer $req
fi
done
First read will determine if something is comming or else end loop.
Second loop with very short timeout will read dropped URL as a single string
read -a req will split string to consider only 1st part