I want a terminal command that will edit a text file by adding 1 to the number in the text.
The entire contents of the text file is just a number.
I'm running the command through appleScript's do shell script command.
Quick and dirty:
do shell script "echo $(( $(<FoO) + 1 )) >FoO"
Related
This question already has answers here:
Looping through the content of a file in Bash
(16 answers)
Closed 1 year ago.
I have a list file of selected pdfs (each line contains a path to pdf). These pdfs are located in different directories which are several thousands in total. I am very new to using bash and struggling to open only those files which are listed in the list file. Any help would be much appreciated.
If there are thousands of files listed, you probably don't want to open them all at once. This little script opens the one you specify on the command line by number. You could modify it to print out the name or search the list, etc.
#!/bin/sh
case "$1" in
[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]) N="$1" ;;
*) echo "Put a file number on the command line"; exit ;;
esac
while read pdfpath; do
if [ $((--N)) = 0 ]; then
atril "$pdfpath" & # Or evince, okular, etc.
exit
fi
done < listfile.txt
The opening case sets N to the first command line argument ($1) if it's a number but exits if a number wasn't specified.
while read ... done loops through the lines in your list file and reads each line in turn in the variable pdfpath.
$((--N)) decrements the variable, so when it gets to zero it matches the comparison and enters the if clause.
The & causes the command to execute but control returns immediately so the script can exit and not tie up your bash prompt.
is it possible to save all the output from a created bash shell file?
Basically the file makes calculations and I would like to save all the output of this created bash shell.
Basically I want to save all the output to a file or sample the last 10 outputs (for example)
$ ./yourscript.sh > outputfile.txt
Where yourscript.sh is the bash shell file you've created and outputfile.txt is the file you want to write the output to.
I trying to append passed text as parameter to file using shell script
this is the code of shell
echo $1>>/etc/freeradius/mods-enabled/ldap
this shell will get the text to add it to the file ldap in /etc/freeradius/mods-enabled path , I call this shell in this format
# sh /etc/append.sh hello how are you man
but in this example the shell only get first word 'hello' and append it to file .how can I tell shell that all words are same variable and should insert to file
It depends on the shell you're using.
In bash the this script should work:
#!/bin/bash
echo "${#:1}">>/etc/freeradius/mods-enabled/ldap
Edit: As DTSCode pointed out in comments the :1 part in the script is redundant so this would be more correct:
#!/bin/bash
echo "$#" >> /etc/freeradius/mods-enabled/ldap
Then give the permissions to execute the file and call
/etc/append.sh hello how are you man
Or without execute permission call
bash /etc/append.sh hello how are you man
I get syntax error and a prompt 'Display all 2733 possibilities? (y or n)' when I paste the following in terminal in ubuntu 14.04. The lines are indented with '\t' characters. No errors if I replace '\t' with space characters. Is the terminal treating '\t' specially and showing options, therefore? need help - What is the problem here?
if [ $x -eq 1 ]; then
cat non-existent-file;
fi
In an interactive session Tab triggers auto-complete. Bash doesn't know that you're pasting text. It sees a copy-and-pasted \t the same as if you pressed Tab on the keyboard.
Normally you type part of a long command name then press Tab to auto-complete the rest of it. If you press Tab at an empty prompt then auto-complete matches every single program in your $PATH, all 2,733 of them.
I have the following script
#!/bin/bash
/usr/bin/osascript << EOT
set myfile to choose file
EOT
no_ext=$(python -c "print '$myfile'.split('.')[0]")
### this works - just need to know how to pass the arg
R CMD Sweave no_ext.Rnw
pdflatex no_ext.tex
open no_ext.pdf
Can anyone point me to "how to pass the variable myfile correctly" ?
EDIT
Thx for all the suggestions!
Don't know what to accept, all of your answers really helped me since I learned a lot from everybody.
The following problems exist in your script:
A variable set in the AppleScript section does become defined in the enclosing shell script. You have to do the data exchange with the shell script by using command substitution.
AppleScripts invoked from a shell script aren't allowed to do user interaction because they do not have an application context. You can use the helper application "AppleScript Runner" to run user interaction commands.
Here is a revised version of your script where those problems are fixed:
#!/bin/bash
myfile=$(/usr/bin/osascript << EOT
tell app "AppleScript Runner"
activate
return posix path of (choose file)
end
EOT)
if [ $? -eq 0 ]
then
echo $myfile
else
echo "User canceled"
fi
First, you need to get the contents of the myfile variable from Applescript to bash. I don't know Applescript, so I'll make a shot in the dark as to how to write to its standard output. Then the python part is just unnecessary complexity (and likely wrong anyway, you were throwing away everything after the first . rather than the last). Next you need a $ before the variable name in bash syntax. I think the following script does what you want:
#!/bin/sh
set -e
myfile=$(osascript <<EOT
set myfile to choose file
write myfile to stdout
EOT
)
no_ext="${myfile%.*}"
R CMD Sweave "$no_ext.Rnw"
pdflatex "$no_ext.tex"
open "$no_ext.pdf"
(set -e at the beginning makes the shell exit immediately if an error occurs, instead of trying to execute pdflatex even though no .tex file has been produced or somesuch.)
Realize that applescript paths are colon ":" delimited. You need slash delimited in bash so in applescript terms that's the "posix path". Also, when using osascript it can't open dialog windows. You must tell an application to open the window. Next, you "return" something from the applescript... that's what goes to bash. Finally, in bash to execute a command and assign the result to a variable use `` around the command. So knowing this here's a shell script to use an applescript to get the myFile variable.
#!/bin/bash
myFile=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfile to choose file with prompt "Select the file to use in bash!"
end tell
return (posix path of myfile)
EOT`
echo $myFile