列出所有cronjobs,如果有任何没有多余的信息

时间:2016-02-18 06:35:33

标签: bash filter grep cron crontab

尝试输出cronjobs列表,而不是用户列表。 sudo script.sh的原始输出太脏了,我似乎无法清理它。我使用susudo script.sh | grep -v no运行此操作,然后运行它。我也试过调用它#!/bin/bash #Trying to show all cronjobs but no extraneous info # # This shows "no crontab for USER" for every USER without # a crontab - I only want to see actual cronjobs, not a long # list of users without crontabs echo "Here is the basic output that needs manipulation: " for USER in `cat /etc/passwd | cut -d":" -f1`; do crontab -l -u $USER done # # grep -v fails me # (grep'ing the output of the script as a whole fails also) echo " trying with grep -v no on each line: " for USER in `cat /etc/passwd | cut -d":" -f1`; do crontab -l -u $USER | grep -v no done echo " maybe with quotes around the no: " for USER in `cat /etc/passwd | cut -d":" -f1`; do crontab -l -u $USER | grep -v "no" done # string manipulation - I can't even get started echo " And here I try to put the commmand output into a string so I can manipulate it further, and use an if/then/fi on the product: " for USER in `cat /etc/passwd | cut -d":" -f1`; do STRING="$(crontab -l -u $USER | grep -v no)" echo "STRING: $STRING" done 。我不明白为什么这不起作用:

public class PicturePlugin extends CordovaPlugin
{
    private String callback="data";
    private int IMAGE_TAKEN=1;
    private CallbackContext callbackContext;    
    private String TAG="FilePlugin";
    private int imageWidth,imageHeight;
    private String imagePath;
    private File destImageFile;
    private String mode = "BACK";
    private static String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    CameraManager manager;
    private static final String TAG1 = null ;

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
    {
        this.callbackContext = callbackContext;
        this.cordova.getActivity().getApplicationContext().getPackageName();        
        try
        {
            JSONObject object=(JSONObject) args.get(0);
            imageWidth=object.getInt("targetWidth");
            imageHeight=object.getInt("targetHeight");
            if(object.has("mode")){
                if(object.getString("mode").equals("FRONT")){
                    mode = "FRONT";
                    openFrontFacingCamera();
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
                }
            }else{
                mode = "BACK";
            }
            Intent camera=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            imagePath = getCapturedImageExternal();
            destImageFile = new File(imagePath);
            camera.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(destImageFile));
            this.cordova.setActivityResultCallback(PicturePlugin.this);
            cordova.getActivity().startActivityForResult(camera,IMAGE_TAKEN);
        }
        catch (Exception e)
        {
            Log.i(TAG, "Exception "+e.getMessage());
            callbackContext.error("failed");
        }
        return true;
    }


    private Camera openFrontFacingCamera()
    {
        int cameraCount = 0;
        Camera cam = null;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        for ( int camId = 0; camId < cameraCount; camId++ ) {
            Camera.getCameraInfo( camId, cameraInfo );
            if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
                try {
                    cam = Camera.open( camId );
                } catch (RuntimeException e) {
                    Log.e(TAG1, "Camera failed to open: " + e.getLocalizedMessage());
                }
            }
        }

        return cam;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        if (requestCode == IMAGE_TAKEN && resultCode == Activity.RESULT_OK) {
            File finalFile = null;
            File fileTobeDeleted = null;
            Bitmap photo = null;
            File sd = new File(Environment.getExternalStorageDirectory(),
                    Constants.GOBIZMO_IMAGE_DIR);
            String destinationImagePath = File.separator 
                    + Constants.TEMP_CAMERA_IMAGE + ".JPEG";
            File destination = new File(sd, destinationImagePath);
            sd.setWritable(true);

            try {
                String encoded;
                imagePath = destImageFile.getAbsolutePath();
                finalFile = new File(imagePath);
                fileTobeDeleted = new File(imagePath);
                int angle = getAngle(finalFile.getAbsolutePath());
                    if (finalFile.exists()) {
                        photo = BitmapFactory.decodeFile(finalFile
                                .getAbsolutePath());
                        Matrix matrix = new Matrix();
                        matrix.postRotate(angle);
                        Bitmap scaledBitmap = Bitmap.createScaledBitmap(photo, imageWidth, imageHeight, true);
                        photo = Bitmap.createBitmap(scaledBitmap, 0, 0, imageWidth, imageHeight, matrix, true);
                        // //////New orientation fix for all
                        // devices///////////////////////
                        try {
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
                            byte[] byteArray = stream.toByteArray();
                            encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
                            Log.e("base 64 image", encoded);
                            JSONObject object = new JSONObject();
                            object.put(callback, encoded);
                            finalFile.delete();
                            fileTobeDeleted.delete();
                            //imagePath = destination.getPath();
                            if(new File(imagePath).exists()){
                                new File(imagePath).delete();
                            }
                            callbackContext.success(encoded);
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.i(TAG, "onActivityResult " + e.getMessage());
                            finalFile.delete();
                            fileTobeDeleted.delete();

                            if(new File(imagePath).exists()){
                                new File(imagePath).delete();
                            }
                            callbackContext.error("failed");

                        } 
                    }
            } catch (Exception exp) {
                exp.printStackTrace();
                Log.i(TAG, "onActivityResult " + exp.getMessage());
                try {
                    finalFile.delete();
                    fileTobeDeleted.delete();
                    imagePath = destination.getPath();
                    if(new File(imagePath).exists()){
                        new File(imagePath).delete();
                    }
                    callbackContext.error("failed");
                }catch(Exception innerexception){
                    innerexception.printStackTrace();
                }
            }
        }
    }

    public Uri getImageUri(Context inContext, Bitmap inImage)
    {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

    public String getRealPathFromURI(Uri uri)
    {
        Cursor cursor = cordova.getActivity().getContentResolver().query(uri, null, null, null, null); 
        cursor.moveToFirst(); 
        int idx = cursor.getColumnIndex(Images.ImageColumns.DATA);
        return cursor.getString(idx); 
    }


    public static String getCapturedImageExternal() {

        // External sdcard location
        File mediaStorageDir = new File(
                android.os.Environment.getExternalStorageDirectory(),
                "Gobizmo image");
        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()){
            mediaStorageDir.mkdirs();
        }
        if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) {
            Log.d(Constants.GOBIZMO_IMAGE_DIR, "Oops! Failed create "
                    + Constants.GOBIZMO_IMAGE_DIR + " directory");
            return null;
        }
        // Create a timestamp

        return mediaStorageDir.getPath() + File.separator + Constants.TEMP_CAMERA_IMAGE
                + ".JPEG";
    }

    public static String getRealPathFromURI(Context context, Uri contentUri) {
        String filepath = "";
        String uriPath = contentUri.toString();
        // Handle local file and remove url encoding
        if (uriPath.startsWith("file://")) {
            filepath = uriPath.replace("file://", "");
            try {
                return URLDecoder.decode(filepath, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        try {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = context.getContentResolver().query(contentUri,
                    projection, null, null, null);
            if (cursor != null && cursor.getCount() != 0) {
                int column_index = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                filepath = cursor.getString(column_index);
            }
        } catch (Exception e) {
            Log.e("Path Error", e.toString());
        }
        return filepath;
    }

    private int getAngle(String path)
    {
        int angle=0;
        try
        {       
            ExifInterface ei = new ExifInterface(path);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            Log.i(TAG, "getAngle "+orientation);

            switch(orientation)
            {
                case ExifInterface.ORIENTATION_ROTATE_90:
                                                            angle=90;
                                                            break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                                                            angle=180;
                                                            break;
                case ExifInterface.ORIENTATION_ROTATE_270 :
                                                            angle=270;
                default:
                                                            angle=0;
            }
        }
        catch(Exception ex)
        {
            Log.i("getAngle", "getAngle :: "+ex.getMessage());
        }
        return angle;
    }

}
顺便说一句,有没有比在每行开头粘贴4个空格更简单的方法来获取正确格式的代码?我必须进行40分钟的实验。不抱怨,只是问。

1 个答案:

答案 0 :(得分:0)

crontab将“no crontab for user”连接到标准错误。要删除这些消息,您可以运行

crontab -l -u $USER 2>/dev/null
在你的循环中

我还建议将USER重命名为其他内容。 USER变量名称是保留的,应设置为您的用户(登录名)。通常,您应该使用小写变量名称,以避免这种名称冲突。